This compares a Customer Name and Part Number on sheet Temp (about 50 rows) to Customer Name and Part Number on sheet Data (about 20,000 rows). If the name and number are found in Data, then the associated information from that same row in Temp is added to Data.
This works great unless a name and number in Temp are not found in Data. When that occurs, a "Subscript out of range" error is generated. To me, it seems like the code is trying to find that value from Temp, and when it cannot find it, it just gives us and throws the error.
Can the code be revised to say, "Hey, if you cannot match a value, it's okay, just skip it and keep going"?
Sub MergeRMAArray()
'##############################################################################
' Creates arrays from "Temp RMA" & "Data" sheets, then compares rows on RMA and when a match occurs,
' pastes values in temp array. After loops, temp array values paste to "Data" sheet.
'##############################################################################
' If when processed there is an error, and the highlighted section states "Subscript out of range", with i+j
' being larger than the rows shown, then one potential error could be that a part on the RMA tab is not
' present in the Data tab, so the macro keeps searching. Will need to try and fix this on the next revision.
'##############################################################################
'##############################################################################
Set Data = Worksheets("Data")
Set Temp = Sheets("Temp RMA")
Data.Activate
Dim arrA, arrB, arrC As Variant
Dim i, j, k, LastRow2 As Long
LastRow = Data.Cells(Cells.Rows.Count, "A").End(xlUp).Row
LastRow2 = Temp.Cells(Cells.Rows.Count, "A").End(xlUp).Row
arrA = Data.Range("A2:B" & LastRow)
arrB = Temp.Range("A2:H" & LastRow2)
ReDim arrC(1 To LastRow - 1, 1 To 4)
For i = LBound(arrB) To UBound(arrB)
j = 0
For k = LBound(arrA) To UBound(arrA)
If (arrB(i, 1) = arrA(i + j, 1) _
And arrB(i, 2) = arrA(i + j, 2) _
And arrC(i + j, 1) = "") Then
arrC(i + j, 1) = arrB(i, 5)
arrC(i + j, 2) = arrB(i, 6)
arrC(i + j, 3) = arrB(i, 7)
arrC(i + j, 4) = arrB(i, 8)
Exit For
End If
j = j + 1
Next k
Next i
Range("W2").Resize(UBound(arrC, 1), UBound(arrC, 2)).Value = arrC
Erase arrA, arrB, arrC
End Sub