Comparing two columns of Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have account numbers in Column A as well in Column E from row 2 onwards. I would like a formula or VBA code that will compare the two rows of numbers and extract those that are unique

your assistance in this regard will be most appreciated
 

Thanks for your reply!

Can you create any other ActiveX objects with your hardware/software setup?
I have no idea unfortunately.

Is your syntax correct? That is, are you using the syntax exactly as I posted it?
Yes it is copied exactly as you posted it.

Are you using a Mac?
Yes, Mac OS X 10.7.4.

What verion of Windows (if you're using Windows) and what version of Excel?
Excel 2011 Version 14.

(I shouldn't really ask this, but just for completeness, were you using a legal copy of Excel?
Yes, the Excel I'm using is provided by the company I work for and is legal.

If the data you need to check are just numbers, rather than text, then an easy and very fast workaround could be used.
Unfortunately I'm using alphanumeric input.

Finally, you can try the following alternative code formulation and see if it does anything for you.
I tried running it and it gives the same error as before.

Any ideas on what I can do to check for errors? Thanks for your support!
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Thanks for your reply!

...

Any ideas on what I can do to check for errors? Thanks for your support!
Those codes I posted won't work on a Mac. It incompatibility rather than error.

But the following should run on a Mac. Try some test data, with one range down column A and the other down column E on Sheet1. And nothing on Sheet2.

Run the code with these test data and see if it produces the sort of result you're wanting.
Code:
Sub jksljk()
Dim lra As Long, lre As Long
Dim rga, rge, ua(), ue(), uboth()
Dim ka As Long, ke As Long, kboth As Long
Dim c, x

Sheets("sheet1").Activate
lra = Range("A" & Rows.Count).End(3).Row - 1
lre = Range("E" & Rows.Count).End(3).Row - 1
rga = Range("A2").Resize(lra)
rge = Range("E2").Resize(lre)
ReDim ua(1 To lra, 1 To 1)
ReDim ue(1 To lre, 1 To 1)
ReDim uboth(1 To lra + lre, 1 To 1)
Application.ScreenUpdating = False
Sheets("sheet2").Activate
With Range("A1").Resize(lra)
    .Value = rga
    .Sort .Cells(1), 1, Header:=xlNo
    x = vbNullString
For Each c In .Cells.Value
    If c <> x Then
        ka = ka + 1
        ua(ka, 1) = c
        x = c
    End If
Next
End With
Sheets("sheet1").Range("G2").Resize(ka) = ua
Sheets("sheet1").Range("G1") = "Uniques in A"

With Range("E1").Resize(lre)
    .Value = rge
    .Sort .Cells(1), 1, Header:=xlNo
    x = vbNullString
For Each c In .Cells.Value
    If c <> x Then
        ke = ke + 1
        ue(ke, 1) = c
        x = c
    End If
Next
End With
Sheets("sheet1").Range("H2").Resize(ke) = ue
Sheets("sheet1").Range("H1") = "Uniques in E"

With Range("A1").Resize(lra + ke)
    .Cells(lra + 1, 1).Resize(ke).Value = ue
    .Sort .Cells(1), 1, Header:=xlNo
    x = vbNullString
For Each c In .Cells.Value
    If c <> x Then
        kboth = kboth + 1
        uboth(kboth, 1) = c
        x = c
    End If
Next
End With
Cells.Resize(, 5).ClearContents
Sheets("sheet1").Range("I2").Resize(kboth) = uboth
Sheets("sheet1").Range("I1") = "Uniques over both"

Sheets("sheet1").Activate
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Thank you so much for that new code! I've gotten the code to run, but it doesn't really do what I would like it to.

[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]10[/TD]
[/TR]
</tbody>[/TABLE]

Given the table above, I would like column C to output the uniques in column A that aren't present in column B. This list would contain the values 1, 4, 8 (note that it doesn't output a blank for row 5, and it doesn't output 1 two times). Similarly, I would like column D to output the uniques in column B that aren't present in column A. This list would contain 6, 3, 4, 9, 10. I would also like to have a count for each, ie. the number of values in column A which aren't present in column B and vice-versa.

It would also be great if the previous data under each header could be removed every time the script is run, if possible.

Thank you so much for your help with this! I hope I've been able to explain myself adequately this time.
 
Upvote 0
Thank you so much for that new code! I've gotten the code to run, but it doesn't really do what I would like it to.

[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]10[/TD]
[/TR]
</tbody>[/TABLE]

Given the table above,] I would like column C to output the uniques in column A that aren't present in column B. This list would contain the values 1, 4, 8 (note that it doesn't output a blank for row 5, and it doesn't output 1 two times). Similarly, I would like column D to output the uniques in column B that aren't present in column A. This list would contain 6, 3, 4, 9, 10. I would also like to have a count for each, ie. the number of values in column A which aren't present in column B and vice-versa.

It would also be great if the previous data under each header could be removed every time the script is run, if possible.

Thank you so much for your help with this! I hope I've been able to explain myself adequately this time.
What do you mean by the red bit above? 4 is present in both A and B, but you say it should go in C ?????
 
Upvote 0
What do you mean by the red bit above? 4 is present in both A and B, but you say it should go in C ?????

Ah sorry about that, I was mistaken and you are correct. Column C should contain 1, 8 and Column D should contain 6, 3, 9 and 10.
 
Upvote 0
Ah sorry about that, I was mistaken and you are correct. Column C should contain 1, 8 and Column D should contain 6, 3, 9 and 10.
I don't know what kind of Excel is on Macs these days. When I had one I used Parallels or VMFusion. Bootcamp wasn't usually worth the trouble.

I found that Microsoft had so limited the functionality in Excel Mac 2003 and 2008, that I quickly gave up on both of them. Not the Mac's fault though. They're great machines.

Anyway, here's a code that's more or less based on first principles, not relying on anything that shouldn't be in all versions of Excel with VBA. Try it and see ... (data in cols A and B, results in D and E, no headers)
Code:
Sub scode()
Dim a, b
Dim ua(), ub()
Dim qa() As Boolean, qb() As Boolean
Dim rwa As Long, rwb As Long
Dim ka As Long, kb As Long
Dim i As Long, j As Long

rwa = Range("A" & Rows.Count).End(3).Row
rwb = Range("B" & Rows.Count).End(3).Row
ReDim ua(1 To rwa, 1 To 1), ub(1 To rwb, 1 To 1)
ReDim qa(rwa), qb(rwb)

a = Range("A1").Resize(rwa)
b = Range("B1").Resize(rwb)

For i = 1 To rwa - 1
    If qa(i) = False Then
    For j = i + 1 To rwa
        If qa(j) = False Then
            If a(i, 1) = a(j, 1) Then qa(j) = True
        End If
    Next j
    End If
Next i

For i = 1 To rwb - 1
    If qb(i) = False Then
    For j = i + 1 To rwb
        If qb(j) = False Then
            If b(i, 1) = b(j, 1) Then qb(j) = True
        End If
    Next j
    End If
Next i

For i = 1 To rwa
    If qa(i) = False Then
    For j = 1 To rwb
        If qb(j) = False Then
            If a(i, 1) = b(j, 1) Then
                qa(i) = True
                qb(j) = True
            End If
        End If
    Next j
    End If
Next i

For i = 1 To rwa
    If qa(i) = False Then ka = ka + 1: ua(ka, 1) = a(i, 1)
Next i

For j = 1 To rwb
    If qb(j) = False Then kb = kb + 1: ub(kb, 1) = b(j, 1)
Next j

If ka > 0 Then Range("D1").Resize(ka) = ua
If kb > 0 Then Range("E1").Resize(kb) = ub
End Sub
 
Upvote 0
Very nice code mate in Post 10.

Biz
 
Upvote 0
Very nice code mate in Post 10.

Biz
Thanks for comment Biz. It's a pretty easy problem if you have access to useful built-in devices like scripting dictionary.

But actually the code in Post#16 was somewhat more challenging, since in that one I wanted to use only the most basic concepts.
 
Upvote 0
Thanks for comment Biz. It's a pretty easy problem if you have access to useful built-in devices like scripting dictionary.

But actually the code in Post#16 was somewhat more challenging, since in that one I wanted to use only the most basic concepts.

I'm using Excel 2003 and get run-time error '13' Type mismatch see item in bold.
[For i = 1 To rwa
If qa(i) = False Then
For j = 1 To rwb
If qb(j) = False Then
If a(i, 1) = b(j, 1) Then
qa(i) = True
qb(j) = True
End If
End If
Next j
End If
Next i Then

Biz
 
Last edited:
Upvote 0
I don't know what kind of Excel is on Macs these days. When I had one I used Parallels or VMFusion. Bootcamp wasn't usually worth the trouble.

I found that Microsoft had so limited the functionality in Excel Mac 2003 and 2008, that I quickly gave up on both of them. Not the Mac's fault though. They're great machines.

Anyway, here's a code that's more or less based on first principles, not relying on anything that shouldn't be in all versions of Excel with VBA. Try it and see ... (data in cols A and B, results in D and E, no headers)
Code:
...

Thank you very much for taking the time to write that out the more complicated way! I really appreciate it! It does the trick, with a few exceptions. My testing list looks like this:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B[/TD]
[TD]Column C[/TD]
[TD]Column D[/TD]
[TD]Column E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]5[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]6[/TD]
[TD][/TD]
[TD]3[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]8[/TD]
[TD][/TD]
[TD]4[/TD]
[TD]8[/TD]
[/TR]
</tbody>[/TABLE]

As you can see, it is successfully disregarding the extra '1' in Column A, but for some reason it is printing out '4' twice. It's also returning the blank value in Column B. I wish I knew how to change the code to consider these exceptions! Hope you have the patience to help me with this :)

Thank you again!
 
Upvote 0

Forum statistics

Threads
1,225,322
Messages
6,184,269
Members
453,224
Latest member
Prasanna arachchi

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