Removing duplicates in an array

EdStockton

New Member
Joined
Aug 6, 2014
Messages
47
Hello everyone,

I am attempting to remove duplicates in three columns individually. The data is a trial balance on a sheet entitled TB. I am selecting columns A, B, and C and copying those columns to a new sheet called Criteria. Once I get the columns to Criteria, I need to remove the duplicates in each column individually. It works except for the last column. My trial balance is about 500 rows so for my example shown here I will display only a few rows. My code is as follows:

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+z
'
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Sheets(1)
Sheets("Sheet1 (2)").Select
Sheets("Sheet1 (2)").Name = "Criteria"
Application.GoTo ActiveWorkbook.Sheets("TB").Cells(1, 1)
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).Select
Selection.Copy
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 1)
ActiveSheet.Paste
Application.CutCopyMode = False
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 1)
OnlyColumnA
Application.GoTo ActiveWorkbook.Sheets("TB").Cells(1, 2)
ActiveSheet.Range("b1", ActiveSheet.Range("b1").End(xlDown)).Select
Selection.Copy
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 2)
ActiveSheet.Paste
Application.CutCopyMode = False
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 2)
OnlyColumnB
Application.GoTo ActiveWorkbook.Sheets("TB").Cells(1, 3)
ActiveSheet.Range("c1", ActiveSheet.Range("c1").End(xlDown)).Select
Selection.Copy
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 3)
ActiveSheet.Paste
Application.CutCopyMode = False
Application.GoTo ActiveWorkbook.Sheets("Criteria").Cells(1, 3)
OnlyColumnC
End Sub
Sub OnlyColumnA()
Dim n As Long

n = Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("A1:A" & n).RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
Sub OnlyColumnB()
Dim m As Long

m = Cells(Rows.Count, "B").End(xlUp).Row
ActiveSheet.Range("B1:B" & m).RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
Sub OnlyColumnC()
Dim p As Long

p = Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C1:C" & p).RemoveDuplicates Columns:=1, Header:=xlYes
End Sub

When I run this, I still have duplicates in column C. The data I am using is of this nature:

PropertyPropTypePropNum
Bond0000Bond0000
OPCP0000OPCO0000
OPMA0000OPMA0000
CFP0000CFP0000
RHF0000RHF0000
Bond1001Bond1001
OPCO1001OPCO1001
OPMA1001OPMA1001
CFP1001CFP1001
RHF1001RHF1001
Bond1005Bond1005
OPCO1005OPCO1005
OPMA1005OPMA1005
CFP1005CFP1005
RHF1005RHF1005
Bond1007Bond1007
OPCO1007OPCO1007
OPMA1007OPMA1007
CFP1007CFP1007
RHF1007RHF1007

<tbody>
</tbody>
<strike></strike>

<strike></strike>When the process is finished it should look like this:

PropertyPropTypePropNum
Bond0000Bond0000
OPCP0000OPCO1001
OPMA0000OPMA1005
CFP0000CFP1007
RHF0000RHF
Bond1001
OPCO1001
OPMA1001
CFP1001
RHF1001
Bond1005
OPCO1005
OPMA1005
CFP1005
RHF1005
Bond1007
OPCO1007
OPMA1007
CFP1007
RHF1007

<tbody>
</tbody>

Instead it looks like this:

PropertyPropTypePropNum
Bond0000Bond0000
OPCP0000OPCO0000
OPMA0000OPMA0000
CFP0000CFP0000
RHF0000RHF0000
Bond10010000
OPCO10010000
OPMA10010000
CFP10010000
RHF10010000
Bond10050000
OPCO10050000
OPMA10050000
CFP10050000
RHF10050000
Bond10070000
OPCO10070000
OPMA10070000
CFP10070000
RHF10070000

<tbody>
</tbody>

I would really appreciate your help.

Thanks, Ed
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this :-
From sheets "TB" to "Criteria"
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Jun22
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
Ray = Sheets("Tb").Cells(1).CurrentRegion
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
ReDim nRay(1 To UBound(Ray, 1), 1 To 3)
[COLOR="Navy"]For[/COLOR] ac = 1 To 3
    [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
        Dic(Ray(n, ac)) = Empty
    [COLOR="Navy"]Next[/COLOR]
        [COLOR="Navy"]With[/COLOR] Sheets("Criteria").Cells(1, ac).Resize(Dic.Count)
            .Value = Application.Transpose(Dic.keys)
            .Parent.Range("C:C").NumberFormat = "@"
        [COLOR="Navy"]End[/COLOR] With
        Dic.RemoveAll
[COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

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