# Compare Columns in Excel

I have 2 columns in Excel. I need to compare both columns and output (maybe in a 3rd column), the values that exist in Column B but do not exist in Column A.

Column A
John Smith
Betsy Smith
Carl Benz

Column B
John Smith
Betsy Smith
Nate Dog

Comparing these 2 columns, for example, I would like the output in Column C to be:

Column C
Nate Dog

It doesn't matter that Karl Benz is in Column A and not in Column B. I only want values that appear in B and not in A.

###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You can use the following code which will do what you are looking for..

Sub copyvalues()
Dim lrow As Long, lr As Long, cr As Long
lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
lr = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Dim rng As Range, r As Range, cell As Range

Set rng = Range("A1:A" & lrow)
Set r = Range("B1:b" & lr)

For Each cell In r

If Application.WorksheetFunction.CountIf(rng, cell.Value) = 0 And cell.Value <> "" Then
If Cells(1, 3).Value = "" Then
cr = 1
Else
cr = Cells(Cells.Rows.Count, 3).End(xlUp).Row + 1
End If

cell.Copy Cells(cr, 3)
End If

Next cell
End Sub

Saurabh...

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
If you don't want to use code, you could use the VLooKUp function to identify what does match, then filter column C for "Blanks".  For instance in your example if the items in your example were in rows 1-3, the in column C you would put the following formula:

=IfError(Vlookup(B1, \$A\$1:\$A\$3, 1, False), "")

Copy the formula down to B3.

Filter Column C for "" or blanks, and you should have your answer.
Author Commented:
Saurabh,
Where would this code be run?

tomfarrar,
It looks like your solution would only work if the matching names were on the same row. My data has instances of matching data on different rows (for example John Doe could be in Column A, Row 3, and he could also appear in Column B, Row 6).
Commented:
Isaacr25,

On your workbook..Press alt+f11 which will be open the visual basic editor..now in their goto insert-->new module--> and copy & paste the code their...

Now close the visual basic editor and come to your worksheet where you want to run this code..press alt+f8 you will see a macro by the name of copyvalues..Just run that macro and it will do what you are looking for...

Saurabh...
Business Systems Analyst , ex-Senior Application EngineerCommented:
I did some research and it seems that it can be done without using macro, but there will be a need to create a temporary column for the solution (pls refer to my work ver 1 as attached, by making assumption there will be no empty repeated values in Column B)

1. For the temporary column (Column C), it's used to find the values that were missing.

with formula like:
= IF( AND(LEN(B2)>0,  ISERROR(  MATCH(B2,A:A,0) )),B2, "")
and then drag it down accordingly for the following cells.

you can hide this column if necessary.

2. For the final column (Column D), it's used to populate the final outcome.

with formula:

1) in Cell D2.
=INDEX(\$C2:\$C\$100,MATCH(TRUE,\$C2:\$C100<>"",0))
to find the first non-empty value of Column C

this need to be an array formula, press ctrl + shift + enter when you put in the formula.

2) in Cell D3.
=IFERROR(   IF(D2="", "",   IF(   MATCH(D2,C:C,0) >= SUM(ROW()),  ""&  INDEX( INDIRECT("\$C"& MATCH(D2,C:C,0)+1&":\$C100"),  MATCH(TRUE, INDIRECT("\$C"& MATCH(D2,C:C,0)+1&":\$C100")<>"",0), 0),                 INDEX(\$C3:\$C\$100,MATCH(TRUE,\$C3:\$C101<>"",0))     )   ), "")
to populate the following missing values, and then drag it down accordingly for the following cells.

this need to be an array formula, press ctrl + shift + enter when you put in the formula.

The formula seems a bit complex but it seems to work for me after few rounds of testing.
28738710.xlsx
Commented:
No my solution does not require the data be in the same row.  The example I used for the second variable in the VLookUp: \$A\$1:\$A\$3 will match the first variable B1 against all names in \$A\$1:\$A\$3.  See attached spreadsheet.
EE.xlsx
Commented:
Sorry, here is a better example.  I didn't realize the rows were the same in the first example.  I just switched a couple of Column B rows around.
EE.xlsx
Older than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.