Macro to extract Unique Account Numbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have account Numbers in Col A and in Col C. I need to compare these and extract into Col G , those that are unique to both Columns


I have tried to write code to compare the account numbers in Col A & C and extract those that are unique to both in Col G, but the code only copies the exact data that appears in Col A & C and not those that are unique to both these columns

When the unique records are extracted in Col G, I would also like the Col Number where the record was extracted to display in Col H

It would be appreciated if someone could kindly amend my code



Code:
 Sub Extract_Unique_ITEMS()
Sheets(14).Select

Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row).AdvancedFilter _
    Action:=xlFilterCopy, CopyToRange:=Range("G2"), Unique:=True
End Sub

See Sample Data of what the unique records should look like once extracted


Excel 2012
ABCDEFGH
1Mapping DataInput Data
2KNR-3004KNR-3024KNR-3004Col A
3KNR-3024KNR-3025KNR-3026Col A
4KNR-3025KNR-3064KNR-3027Col A
5KNR-3026KNR-3065KNR-3029Col A
6KNR-3027KNR-3066KNR-3067Col A
7KNR-3029KNR-3304KNR-3304Col B
8KNR-3064KNR-3305KNR-3305Col B
9KNR-3065KNR-3306KNR-3306Col B
10KNR-3066KNR-3514KNR-3514Col B
11KNR-3067KNR-3524KNR-3524Col B
12
13
14
15
Sheet1
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
See if this works..
Code:
Sub Extract_Unique_ITEMS()Dim rng As Range
Dim rng2 As Range
Dim lastrow As Long
Dim counter As Long
Dim thiscell As Range
    Sheets(14).Select
    With ActiveSheet
        lastrow = ActiveSheet.Cells(1048576, 1).End(xlUp).Row
    End With
    Set thiscell = Range("G2")
    counter = 0
    For Each rng In Range(Range("A2"), Range("A" & lastrow))
        For Each rng2 In Range(Range("C2"), Range("C" & lastrow))
            If rng.Value <> rng2.Value Then
                counter = counter + 1
            Else
                Exit For
            End If
        Next
        If counter = lastrow - 1 Then
            thiscell.Value = rng.Value
            thiscell.Offset(0, 1).Value = "Col A"
            Set thiscell = thiscell.Offset(1, 0)
        End If
        counter = 0
    Next
    For Each rng In Range(Range("C2"), Range("C" & lastrow))
        For Each rng2 In Range(Range("A2"), Range("A" & lastrow))
            If rng.Value <> rng2.Value Then
                counter = counter + 1
            Else
                Exit For
            End If
        Next
        If counter = lastrow - 1 Then
            thiscell.Value = rng.Value
            thiscell.Offset(0, 1).Value = "Col C"
            Set thiscell = thiscell.Offset(1, 0)
        End If
        counter = 0
    Next
End Sub
 
Upvote 0
Here is a more compact macro (which I believe is faster) that I think will also work (assuming where you wrote Col B that you meant Col C)...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetUniques()
  Dim X As Long, A As String, C As String, AA As Variant, CC As Variant, Uniq As Variant
  Sheets(14).Select
  AA = Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp)))
  CC = Application.Transpose(Range("C2", Cells(Rows.Count, "C").End(xlUp)))
  A = " " & Join(AA) & " "
  C = " " & Join(CC) & " "
  For X = 1 To UBound(AA)
    C = Replace(C, " " & AA(X) & " ", "  ")
  Next
  C = Replace(Application.Trim(C), " ", "|Col_C ") & "|Col_C"
  For X = 1 To UBound(CC)
    A = Replace(A, " " & CC(X) & " ", "  ")
  Next
  A = Replace(Application.Trim(A), " ", "|Col_A ") & "|Col_A"
  Uniq = Split(Application.Trim(A & " " & C))
  Columns("G:H").ClearContents
  Range("G2").Resize(UBound(Uniq) + 1) = Application.Transpose(Uniq)
  Columns("G").TextToColumns Range("G1"), xlDelimited, , , 0, 0, 0, 0, 1, "|"
  Columns("H").Replace "_", " ", xlPart
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Thanks Rick, this is perfect


I have another scenario where I want to compare account numbers in Col A with Col C and extract the account numbers in Col A that do not appear in Col C in Col J

It would be appreciated if you could kindly assist me
 
Upvote 0
I have account Numbers in Col A and in Col C. I need to compare these and extract into Col G , those that are unique to both Columns


I have tried to write code to compare the account numbers in Col A & C and extract those that are unique to both in Col G, but the code only copies the exact data that appears in Col A & C and not those that are unique to both these columns

When the unique records are extracted in Col G, I would also like the Col Number where the record was extracted to display in Col H

It would be appreciated if someone could kindly amend my code



Code:
 Sub Extract_Unique_ITEMS()
Sheets(14).Select

Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row).AdvancedFilter _
    Action:=xlFilterCopy, CopyToRange:=Range("G2"), Unique:=True
End Sub

See Sample Data of what the unique records should look like once extracted

Excel 2012
ABCDEFGH
1Mapping DataInput Data
2KNR-3004KNR-3024KNR-3004Col A
3KNR-3024KNR-3025KNR-3026Col A
4KNR-3025KNR-3064KNR-3027Col A
5KNR-3026KNR-3065KNR-3029Col A
6KNR-3027KNR-3066KNR-3067Col A
7KNR-3029KNR-3304KNR-3304Col B
8KNR-3064KNR-3305KNR-3305Col B
9KNR-3065KNR-3306KNR-3306Col B
10KNR-3066KNR-3514KNR-3514Col B
11KNR-3067KNR-3524KNR-3524Col B
12
13
14
15

<tbody>
</tbody>
Sheet1


Hi Howard,

Here is more Compact and faster approach:

Code:
Sub howard()
Dim rng     As Range
Dim dict    As Object

Set dict = CreateObject("scripting.dictionary")
Set rng = Range("A2:C" & Cells(Rows.Count, 1).End(xlUp).Row)

With dict
For Each cell In rng
    If Application.CountIf(rng, cell) = 1 Then
        .Item(cell.Value2) = "Col " & Split(Cells(1, cell.Column).Address, "$")(1)
    End If
Next
Range("I2").Resize(.Count) = Application.Transpose(.keys)
Range("J2").Resize(.Count) = Application.Transpose(.items)
End With
End Sub
 
Last edited:
Upvote 0
Thanks for the code, it is lightening fast
 
Upvote 0
Thanks Rick, this is perfect


I have another scenario where I want to compare account numbers in Col A with Col C and extract the account numbers in Col A that do not appear in Col C in Col J

It would be appreciated if you could kindly assist me


Try this:

Code:
Sub howard()
Dim ele         As Variant
Dim ar          As Variant
Dim dict        As Object

Set dict = CreateObject("scripting.dictionary")
ar = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)

With dict
For Each ele In ar
    If Application.CountIf(Range("C2:C" & Cells(Rows.Count, 1).End(xlUp).Row), ele) = 0 Then
        .Item(ele) = Empty
    End If
Next
Range("J2").Resize(.Count) = Application.Transpose(.keys)
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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