Removing duplicates values via VBA

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I have the following spreadsheet:

Code:
[TABLE="width: 514"]
<tbody>[TR]
[TD]Where[/TD]
[TD]Who[/TD]
[TD]Time[/TD]
[TD]CardNum[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Smith, Brian[/TD]
[TD="align: right"]6:52:00[/TD]
[TD="align: right"]2498[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Smith, Brian[/TD]
[TD="align: right"]8:33:00[/TD]
[TD="align: right"]2498[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Smith, Brian[/TD]
[TD="align: right"]7:38:00[/TD]
[TD="align: right"]2498[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Doe, John[/TD]
[TD="align: right"]11:59:00[/TD]
[TD="align: right"]41880[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Doe, John[/TD]
[TD="align: right"]17:23:00[/TD]
[TD="align: right"]41880[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Miller, Juan[/TD]
[TD="align: right"]6:54:00[/TD]
[TD="align: right"]13350[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Miller, Juan[/TD]
[TD="align: right"]14:26:00[/TD]
[TD="align: right"]13350[/TD]
[/TR]
</tbody>[/TABLE]

I am trying to remove all duplicate entries and leave only the earliest occurence on column Time, so my data would look like this:

Code:
[TABLE="width: 514"]
<tbody>[TR]
[TD]Where[/TD]
[TD]Who[/TD]
[TD]Time[/TD]
[TD]CardNum[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Smith, Brian[/TD]
[TD="align: right"]6:52:00[/TD]
[TD="align: right"]2498[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Doe, John[/TD]
[TD="align: right"]11:59:00[/TD]
[TD="align: right"]41880[/TD]
[/TR]
[TR]
[TD]Building 2  Entry A[/TD]
[TD]Miller, Juan[/TD]
[TD="align: right"]6:54:00[/TD]
[TD="align: right"]13350[/TD]
[/TR]
</tbody>[/TABLE]

I have thousands of rows so I can't really do it by hand, the Remove Duplicates command does not really work for me.

Any help will be appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
Sub Macro2()
'
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("A2:A8"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("B2:B8"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C2:C8"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A1:D8")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Range("$A$1:$D$8").RemoveDuplicates Columns:=Array(1, 2, 4), _
        Header:=xlYes
    Range("E13").Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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