Compare two lists

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this

Code:
Sub Compare2Lists()


    ' Get the last row of your lists - assuming in Column A and B - Change accordingly
    L1LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    L2LR = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    ' Set Named Ranges firstList and secondList
    ActiveWorkbook.Names.Add Name:="firstList", RefersTo:="=Sheet1!$A$1:$A$" & L1LR
    ActiveWorkbook.Names.Add Name:="secondList", RefersTo:="=Sheet1!$B$1:$B$" & L1LR
    ' Add Conditional Formatting on firstList
    Range("A1:A" & L1LR).FormatConditions.Add Type:=xlExpression, Formula1:= "=COUNTIF(secondList,A1)=0"
    Range("A1:A" & L1LR).Select
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ' Set Color 1
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Add Conditional Formatting on secondList
    Range("B1:B" & L2LR).FormatConditions.Add Type:=xlExpression, Formula1:= "=COUNTIF(firstList,B1)=0"
    Range("B1:B" & L2LR).Select
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ' Set Color 2
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False


End Sub
 
Last edited:
Upvote 0
Thank you so much. It was very useful for my task.

Best regards!

Glad it worked for you, just in case you did not catch it, there was a typo in the previous code. My apologies, I just saw it when reading your reply.

Code:
[SIZE=3]Sub Compare2Lists()

    ' Get the last row of your lists - assuming in Column A and B - Change accordingly
    L1LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    L2LR = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    ' Set Named Ranges firstList and secondList
    ActiveWorkbook.Names.Add Name:="firstList", RefersTo:="=Sheet1!$A$1:$A$" & L1LR
[U][B]ActiveWorkbook.Names.Add Name:="secondList", RefersTo:="=Sheet1!$B$1:$B$" & [COLOR=#ff0000]L2LR[/COLOR][/B][/U]
    ' Add Conditional Formatting on firstList
    Range("A1:A" & L1LR).FormatConditions.Add Type:=xlExpression, Formula1:= "=COUNTIF(secondList,A1)=0"
    Range("A1:A" & L1LR).Select
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ' Set Color 1
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Add Conditional Formatting on secondList
    Range("B1:B" & L2LR).FormatConditions.Add Type:=xlExpression, Formula1:= "=COUNTIF(firstList,B1)=0"
    Range("B1:B" & L2LR).Select
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ' Set Color 2
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

End Sub[/SIZE]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top