As I understand it, you want to scan through the raw data records, check if the raw data record corresponds to an existing customer record. If it does, then you update the existing customer record. If it does not correspond to an existing customer record then you want to add a new customer record.
What slows down your approach is the repeated use of Select, Copy and Paste. Things will go much faster if you use variables. Read both sheets into variant arrays and then do the comparisons there as in the sample code below.
Option Explicit
Sub AddRecords()
Dim vRaw As Variant
Dim vCustomers As Variant
Dim rgRaw As Range
Dim rgCustomers As Range
Dim lCustRow As Long, lCustCol As Long
Dim lRawRow As Long, lRawCol As Long
Dim blnFound As Boolean
Set rgRaw = ActiveWorkbook.Worksheets("raw data").Cells(1, 1).CurrentRegion
vRaw = rgRaw ' Retrieve entire range of raw data
Set rgCustomers = ActiveWorkbook.Worksheets("customers").Cells(1, 1).CurrentRegion
vCustomers = rgCustomers ' Retrieve entire range of customer data
For lRawRow = 2 To UBound(vRaw, 1) ' Go through each row in the raw data
blnFound = False
For lCustRow = 2 To UBound(vCustomers, 1) ' Compare raw row to each customer row
If vCustomers(lCustRow, 2) = vRaw(lRawRow, 7) Then ' Compare col B to col G as per your code
blnFound = True
Exit For
End If
Next lCustRow
If blnFound Then
' Update the existing customer row
' I don't quite follow the logic of your code but something like the following is needed
vCustomers(lCustRow, 3) = vRaw(lRawRow, 8)
vCustomers(lCustRow, 4) = vRaw(lRawRow, 9)
Else
' Expand the customer array to accommodate the new row
lCustRow = UBound(vCustomers, 1) + 1
' VBA doesn't let you expand Redim Preserve anything but the last dimension of an array, _
so expand it by manually
vCustomers = RedimFirstDimension(vCustomers, lCustRow)
' Populate the new row in the customer array
' I don't quite follow the logic of your code but something like the following is needed
vCustomers(lCustRow, 2) = vRaw(lRawRow, 7)
End If
Next
' Write the customer array back to the customers sheet
' Resize the range since you may have added new customers
Set rgCustomers = rgCustomers.Resize(UBound(vCustomers, 1), UBound(vCustomers, 2))
' Copy back the updated array
rgCustomers = vCustomers
End Sub
Function RedimFirstDimension(vArray As Variant, nrRows As Long) As Variant
' Change the first dimension of the array
Dim lRow As Long
Dim lCol As Long
Dim vNew As Variant
ReDim vNew(LBound(vArray, 1) To nrRows, LBound(vArray, 2) To UBound(vArray, 2))
For lRow = LBound(vArray, 1) To UBound(vArray, 1)
For lCol = LBound(vArray, 2) To UBound(vArray, 2)
vNew(lRow, lCol) = vArray(lRow, lCol)
Next lCol
Next lRow
RedimFirstDimension = vNew
End Function
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…