Compare 2 columns for non-matching numbers

Rochelle B

New Member
Joined
Jul 27, 2004
Messages
5
Hi everyone

I haven't been on this board for about 18 months. It looks great and I've already found answers to some other questions. :biggrin: This site is so helpful I want to kiss my computer at times (pretty sad, eh..) :cry:

My current dilemma... I have two columns of phone numbers and I want to find and isolate (move to another column/worksheet) the numbers in one column that do not appear in the other. (In a perfect world the records would be the same - I'm doing a reconciliation of sorts...)

I know one of you will be able to help.. My levels of frustration :oops: are at your mercy... :pray:

Ta
Rochelle
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi Rochelle, welcome (back?) to the board!
Which columns are the phone numbers in? (Are they on the same sheet?)
And where would you like to put the ones that don't match?

Dan
 
Upvote 0
Hi!
Heres one solution to that.
This checks if the entry in B is in column A. if it find no match then it will write that value in column C.
Book1
ABCD
1221-4005221-4005 
2221-4006220-4006220-4006
3221-4007221-4006 
4221-4008221-356221-356
5221-4009221-4011 
6221-4010221-4012221-4012
7221-4011221-4013221-4013
Sheet2
 
Upvote 0
Thanks!

Dan, for the purposes of this exercise, assume ColumnA and ColumnC contain the lists of numbers, and I'd like to put the exceptions in ColumnE.

I tried the other formula and nothing happened... I'm probably doing something wrong...

Rochelle
 
Upvote 0
Hi SixthSense

That second formula seems to work a treat! Thank you. :biggrin: I'll probably have another question shortly...

R
 
Upvote 0
OK, then using SIXTH SENSE's fine formula you would enter this in E1:
=IF(COUNTIF(A:A,C1)=0,C1,"")
and copy this down Col. E as far as needed.

Myself, I was thinking of some VBA (as I suck at formulas and therefore tend to go the other route), but this looks like it would work just dandy.
It will show in Col. E just the numbers in Col. C that don't exist in Col. A.

This get it done for you?
Dan
 
Upvote 0
Dan, you and Sixth Sense are my new best friends! And, hey, I stink at formulas AND VBA (but I can't leave a problem unsolved so I usually work things out myself). So if you want to write some code to further enhance my education, I'd be only too happy to give it a go!

R :) :wink: :pray:

Oh, and one more thing - What if I now want to count the results? How do I separately count those records that were missing from columnA and ColumnB??

:rolleyes:
 
Upvote 0
Well, I don't know what this will do for anyone's education as there are more efficient ways to get things done than using loops (and it's easier for me to find numbers that are there than ones that aren't ...) but this is one way to get it done. If you don't have tens of thousands of cells to go through the speed shouldn't be much of an issue.
I assume the "newest" list (and therefore likely the longest of the 2) is the list in Col.C (?) If not then we'll have to make a few minor changes...
Code:
Sub FindTheNewNumbers()
Application.ScreenUpdating = False
Dim i As Range
Dim j As Range
Set i = Range("A1", Range("A1").End(xlDown))
Set j = Range("C1", Range("C1").End(xlDown))
       
       For Each x In i
         For Each y In j
             If x.Value = y.Value Then
                x.Copy y.Offset(, 2)
             End If
         Next y
       Next x

Count = 0
For Each cell In Range("C1", Range("C65536").End(xlUp))
    If cell.Offset(, 2) <> "" Then
    cell.Offset(, 2) = ""
    Else:
    cell.Offset(, 2) = cell.Value
    Count = Count + 1
    End If
    Next cell

Application.ScreenUpdating = True

MsgBox ("There are " & Count & " phone numbers in Column C  that are not in Column A"), , "Differences"

End Sub
Try this out and see if it would work for you.
Dan
 
Upvote 0
HalfAce said:
Well, I don't know what this will do for anyone's education as there are more efficient ways to get things done than using loops (and it's easier for me to find numbers that are there than ones that aren't ...) but this is one way to get it done. If you don't have tens of thousands of cells to go through the speed shouldn't be much of an issue.
I assume the "newest" list (and therefore likely the longest of the 2) is the list in Col.C (?) If not then we'll have to make a few minor changes...
Code:
Sub FindTheNewNumbers()
Application.ScreenUpdating = False
Dim i As Range
Dim j As Range
Set i = Range("A1", Range("A1").End(xlDown))
Set j = Range("C1", Range("C1").End(xlDown))
       
       For Each x In i
         For Each y In j
             If x.Value = y.Value Then
                x.Copy y.Offset(, 2)
             End If
         Next y
       Next x

Count = 0
For Each cell In Range("C1", Range("C65536").End(xlUp))
    If cell.Offset(, 2) <> "" Then
    cell.Offset(, 2) = ""
    Else:
    cell.Offset(, 2) = cell.Value
    Count = Count + 1
    End If
    Next cell

Application.ScreenUpdating = True

MsgBox ("There are " & Count & " phone numbers in Column C  that are not in Column A"), , "Differences"

End Sub
Try this out and see if it would work for you.
Dan


Hi Ace,
Your code takes a lot of time in checking each value in A for each value in B. Meaning you will have ColumnA*ColumnB number of loops. and if you will also check B against A, then, thats twice as much.

I think this runs faster.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CopyUnique()
      <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> Range("A65536").End(xlUp).Row
           <SPAN style="color:#00007F">If</SPAN> Application.WorksheetFunction.CountIf(Range("b:b"), Range("a" & i)) = 0 <SPAN style="color:#00007F">Then</SPAN>
                Range("d" & 1 + Range("d65536").End(xlUp).Row) = Range("A" & i)
           <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
      <SPAN style="color:#00007F">Next</SPAN> i
      <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> Range("B65536").End(xlUp).Row
           <SPAN style="color:#00007F">If</SPAN> Application.WorksheetFunction.CountIf(Range("A:A"), Range("B" & i)) = 0 <SPAN style="color:#00007F">Then</SPAN>
                Range("E" & 1 + Range("E65536").End(xlUp).Row) = Range("B" & i)
           <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
      <SPAN style="color:#00007F">Next</SPAN> i

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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