Compare Lists and Add Missing Items

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi,

Is there a way I can compare the items in Column B to those in Column A. Then any item in Column A that's not in Column B would be added to the next available row in Column B.

Thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about
Code:
Sub CompareCols()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Range("B2", Range("B" & Rows.count).End(xlUp))
         If .exists(Cl.Value) Then .Remove Cl.Value
      Next Cl
      Range("B" & Rows.count).End(xlUp).Offset(1).Resize(.count).Value = Application.Transpose(.keys)
   End With
End Sub
 
Upvote 0
It's telling me that C1 = Nothing. I'm guessing that C1 is supposed to equal the items in column A?
 
Upvote 0
In what way is it telling you that Cl =nothing?
 
Upvote 0
Where has the code got to when you get that?
 
Upvote 0
It errors on that line. I did make the one change to include the worksheet, because the lists are on different sheets.

For Each Cl In Worksheets("Sheet1").Range("A2", Range("A" & Rows.count).End(xlUp))
 
Upvote 0
In that case you need to qualify the second part of the range
Code:
For Each Cl In Worksheets("Sheet1").Range("A2", Worksheets("Sheet1").Range("A" & Rows.count).End(xlUp))
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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