Compare Two Columns

    This coding enables the user to compare two columns:

  • By Using VLookup function
  • By Using ERR function
  • If error number is greater than one then application returns the required text
  •  

    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_Error_Number()

    On Error Resume Next

    '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

    'Declare the Range for table Array
    Dim LookupRng As Range
    Set LookupRng = Sh.Range(Cells(2, 2), Cells(Lastrow, 2))

    Dim R As Integer 'Loop variable
    Dim Criteria As String 'Lookup Value

    ' Using For Loop to Loop all the rows in column A
    For R = 2 To Lastrow

    Criteria = Sh.Cells(R, 1).Value
    'If Data exists it returns the Output
    Sh.Cells(R, 3).Value = Application.WorksheetFunction.VLookup(Criteria, LookupRng, 1, 0)

    'If data doesn't exists it returns the error
    '... on Error resume next ... useful here
    If Err.Number > 0 Then
    Sh.Cells(R, 3).Value = "Data Doesn't Exists"
    Err.Clear 'Clear the Error
    End If

    Next
    MsgBox "Comparision Completed"
    End Sub

     

     

    Sub DataExistsInSecondOnly_Based_On_Error_Number()

    On Error Resume Next

    '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 D
    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 B
    Dim StartRow As Integer
    StartRow = 2

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

    'Declare the Range for table Array
    Dim LookupRng As Range
    Set LookupRng = Sh.Range(Cells(2, 1), Cells(Lastrow, 1))

    Dim R As Integer 'Loop variable
    Dim Criteria As String 'Lookup Value

    'Using For Loop to Loop all the rows in column A
    For R = 2 To Lastrow

    Criteria = Sh.Cells(R, 2).Value

    'If Data exists it returns the Output
    Sh.Cells(R, 4).Value = Application.WorksheetFunction.VLookup(Criteria, LookupRng, 1, 0)

    'If data doesn't exists it returns the error
    '... on Error resume next ... useful here
    If Err.Number > 0 Then
    Sh.Cells(R, 4).Value = "Data Doesn't Exists"
    Err.Clear 'Clear the Error
    End If

    Next
    MsgBox "Comparision Completed"
    End Sub

     

     

    Part 1: Comarisiton with VLookup and Error Functions

    Part 2: Comarisiton with VLookup and IFError Functions