Compare two data sets using VLookup and IfError function
Sub DataExistsInFirstOnly_Based_On_IFError()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Sheets("Sheet2")
Dim LRow As Integer
LRow = Sh.Range("C" & Rows.Count).End(xlUp).Row
If LRow > 1 Then
Sh.Range("C2:C" & LRow).ClearContents
Application.Wait (Now + TimeValue("00:00:01"))
End If
Dim StartRow As Integer
StartRow = 2
Dim Lastrow As Integer
Lastrow = Sh.Range("A" & Rows.Count).End(xlUp).Row
Dim LookupRng As String
LookupRng = Sh.Range(Cells(2, 2), Cells(Lastrow, 2)).Address(True, True)
Dim Criteria As String 'Lookup Value
Criteria = Sh.Cells(StartRow, 1).Address(False, False)
Sh.Cells(StartRow, 3).Value = "=iferror(Vlookup(" & Criteria & "," & LookupRng & "," & 1 & "," & 0 & ")," & """Data Doesn't Exists""" & ")"
Sh.Range(Cells(StartRow, 3), Cells(Lastrow, 3)).FillDown
MsgBox "Comparision Completed"
End Sub
Sub DataExistsInSecondOnly_Based_On_IFError()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Sheets("Sheet2")
Dim LRow As Integer
LRow = Sh.Range("D" & Rows.Count).End(xlUp).Row
If LRow > 1 Then
Sh.Range("D2:D" & LRow).ClearContents
Application.Wait (Now + TimeValue("00:00:01"))
End If
Dim StartRow As Integer
StartRow = 2
Dim Lastrow As Integer
Lastrow = Sh.Range("B" & Rows.Count).End(xlUp).Row
Dim LookupRng As String
LookupRng = Sh.Range(Cells(2, 1), Cells(Lastrow, 1)).Address(True, True)
Dim Criteria As String 'Lookup Value
Criteria = Sh.Cells(StartRow, 2).Address(False, False)
Sh.Cells(StartRow, 4).Value = "=iferror(Vlookup(" & Criteria & "," & LookupRng & "," & 1 & "," & 0 & ")," & """Data Doesn't Exists""" & ")"
Sh.Range(Cells(StartRow, 4), Cells(Lastrow, 4)).FillDown
MsgBox "Comparision Completed"
End Sub