I want to compare data from column D in fortest1.xlsx with column F in tested.xlsm. If they are matched, then do nothing. If they are not matched, then copy Column A to C in fortest.xlsx to Column C to E in tested.xlsm. May I know how can I achieve this? My code is running without error, however, it doesn't generate any result.
Thanks in advanced.
Sub test()
'Application.ScreenUpdating = True Dim WbA As Workbook Set WbA = thisworkbook
Dim WbB As Workbook
Set WbB = Workbooks.Open(Filename:="C:\Users\maggie\Desktop\fortest1.xlsx")
Dim SheetA As Worksheet
Dim SheetB As Worksheet
Set SheetA = WbA.Sheets("up")
Set SheetB = WbB.Sheets("up")
Dim eRowA As Integer
Dim eRowB As Integer
eRowA = SheetA.Range("F" & Rows.Count).End(xlUp).Row 'Last line with data in Workbook A (ActiveWorkbook)
eRowB = SheetB.Range("D" & Rows.Count).End(xlUp).Row 'Last line with data in Workbook B (Opened Workbook)
Dim match As Boolean
Dim erow As Long
Dim i, j As Long
Dim r1, r2 As Range
For i = 1 To eRowA
Set r1 = SheetA.Range("F" & i)
match = False
For j = 1 To eRowB
Set r2 = SheetB.Range("D" & j)
If r1 = r2 Then
match = True
End If
Next j
If Not match Then
erow = Range("C" & Rows.Count).End(xlUp).Row + 1
SheetB.Range("A" & j & ":C" & j).Copy Destination:=SheetA.Range("C" & erow & ":E" & erow)
End If
Next i
WbB.Close (False)
End Sub enter image description here