VBA开发需求:将符合条件的行复制至Core_Cutter_List汇总表
VBA Script to Automate Material Request Summary
Got it, let's build that VBA script to automate your material request summary. Here's a reliable solution that does exactly what you need—copy rows from sheet "2" to "Core_Cutter_List" when column A has text and column G is blank:
The Code
Sub CopyToCoreCutterList() Dim sourceWS As Worksheet Dim targetWS As Worksheet Dim lastRow As Long Dim targetLastRow As Long Dim i As Long ' Set references to your worksheets (update names if they change) Set sourceWS = ThisWorkbook.Worksheets("2") Set targetWS = ThisWorkbook.Worksheets("Core_Cutter_List") ' Speed up code by disabling screen updates Application.ScreenUpdating = False ' Find the last used row in column A of the source sheet lastRow = sourceWS.Cells(sourceWS.Rows.Count, "A").End(xlUp).Row ' Loop through each row in the source sheet For i = 1 To lastRow ' Check if column A has content AND column G is empty If sourceWS.Cells(i, "A").Value <> "" And sourceWS.Cells(i, "G").Value = "" Then ' Find the next blank row in the target sheet targetLastRow = targetWS.Cells(targetWS.Rows.Count, "A").End(xlUp).Row + 1 ' Copy the entire row from source to target sourceWS.Rows(i).Copy Destination:=targetWS.Rows(targetLastRow) ' Optional: Mark the row as processed (uncomment to use) ' sourceWS.Cells(i, "G").Value = "Copied to Core List" End If Next i ' Re-enable screen updates Application.ScreenUpdating = True ' Let you know when it's done MsgBox "Success! Matching rows copied to Core_Cutter_List.", vbInformation End Sub
Key Details & Customization
- Worksheet References: Using
ThisWorkbook.Worksheetsensures we're working with the current workbook, so you don't have to worry about accidentally targeting other open files. - Efficiency Toggle: Disabling
ScreenUpdatingmakes the code run way faster without all the annoying screen flicker—we turn it back on at the end, always! - Row Validation: The check
sourceWS.Cells(i, "A").Value <> "" And sourceWS.Cells(i, "G").Value = ""strictly enforces your criteria: only rows with text in A and nothing in G get copied. - Processed Row Marking: If you want to avoid copying the same row multiple times, uncomment the line that writes "Copied to Core List" into column G of the source sheet. This marks the row as handled so it won't get picked up in future runs.
How to Use This
- Open your Excel workbook and press
Alt + F11to open the VBA Editor. - In the left-hand Project Explorer, right-click your workbook name and select Insert > Module.
- Paste the code into the new module window.
- Run the code by pressing
F5, or go back to Excel and add a button to the ribbon/worksheet to trigger the macro with a click (great for non-VBA users!).
Quick Notes
- Make sure the sheets "2" and "Core_Cutter_List" exist in your workbook—if their names change, update those strings in the code.
- If your data starts at row 2 (with headers in row 1), adjust the loop to start at
i = 2instead ofi = 1to skip the header row.
内容的提问来源于stack exchange,提问作者Melnemac32
相关产品推荐
相关产品推荐

