Compare two data sets using VLookup and IfError function

    This coding enables the user to compare two columns:

  • By Using VLookup function
  • By Using IFERROR function
  • If error happend application returns the text which user mentions in IFError function
  •  

    We can do the comparision in two ways:

     

  • Data exists in first set not in second
  • Data exists in second set and not in first
  •  

     

    Download The Workbook

    Sub DataExistsInFirstOnly_Based_On_IFError()

    'Define the variable for activesheet
    Dim Sh As Worksheet
    Set Sh = ActiveWorkbook.Sheets("Sheet2")

    'Define the Last row clear the data exists in column C
    Dim LRow As Integer
    LRow = Sh.Range("C" & Rows.Count).End(xlUp).Row

    'Clear the content in column c
    If LRow > 1 Then
    Sh.Range("C2:C" & LRow).ClearContents
    Application.Wait (Now + TimeValue("00:00:01"))
    End If

    'Define the Start Row of the data in column A
    Dim StartRow As Integer
    StartRow = 2

    'Find the last row in column A
    Dim Lastrow As Integer
    Lastrow = Sh.Range("A" & Rows.Count).End(xlUp).Row

    'Define the Address for table Array
    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()

    'Define the variable for activesheet
    Dim Sh As Worksheet
    Set Sh = ActiveWorkbook.Sheets("Sheet2")

    'Define the Last row clear the data exists in column C
    Dim LRow As Integer
    LRow = Sh.Range("D" & Rows.Count).End(xlUp).Row

    'Clear the content in column c
    If LRow > 1 Then
    Sh.Range("D2:D" & LRow).ClearContents
    Application.Wait (Now + TimeValue("00:00:01"))
    End If

    'Define the Start Row of the data in column A
    Dim StartRow As Integer
    StartRow = 2

    'Find the last row in column A
    Dim Lastrow As Integer
    Lastrow = Sh.Range("B" & Rows.Count).End(xlUp).Row

    'Define the Address for table Array
    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

     

    Part 1: Comarisiton with VLookup and Error Functions

    Part 2: Comarisiton with VLookup and IFError Functions