vba Remove duplicates but keep one

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
675
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Looking to remove dupes (row 3) in several columns (B:AUI) but keeping the first one listed.
Thank you.

NBA.xlsm
ABCDEFGHIJKLMNOPQRST
1VISITORPhiladelphiaLA LakersOrlandoWashingtonHoustonNew OrleansChicagoClevelandNew YorkOklahoma CityCharlotteDenverDallasPortlandMilwaukeeLA ClippersNew OrleansSan AntonioChicago
2HOMEBostonGolden StateDetroitIndianaAtlantaBrooklynMiamiTorontoMemphisMinnesotaSan AntonioUtahPhoenixSacramentoPhiladelphiaLA LakersCharlotteIndianaWashington
3DATE10/18/2210/18/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/19/2210/20/2210/20/2210/21/2210/21/2210/21/22
2022-2023
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Is this what you mean? Test with a copy of your data.

VBA Code:
Sub Remove_Dupes()
  Dim c As Long
  
  Application.ScreenUpdating = False
  For c = Cells(3, Columns.Count).End(xlToLeft).Column To 3 Step -1
    If Cells(3, c).Value = Cells(3, c - 1).Value Then Columns(c).Delete
  Next c
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here's what I thought you wanted i.e. clear the contents if there's a duplicate:

VBA Code:
Option Explicit
Sub Macro1()

    Dim cln As New Collection
    Dim i As Long
    Dim wsSrc As Worksheet
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("2022-2023")
    
    On Error Resume Next
        For i = 2 To wsSrc.Cells(3, Columns.Count).End(xlToLeft).Column
            cln.Add CStr(wsSrc.Cells(3, i)), wsSrc.Cells(3, i)
            If Err.Number <> 0 Then
                wsSrc.Cells(3, i).ClearContents
            End If
            Err.Clear
        Next i
    On Error GoTo 0
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Is this what you mean? Test with a copy of your data.

VBA Code:
Sub Remove_Dupes()
  Dim c As Long
 
  Application.ScreenUpdating = False
  For c = Cells(3, Columns.Count).End(xlToLeft).Column To 3 Step -1
    If Cells(3, c).Value = Cells(3, c - 1).Value Then Columns(c).Delete
  Next c
  Application.ScreenUpdating = True
End Sub
its deleting the entire column. can it only clear.contents of the cell?
below is how I would like it to look.

2022-23-NBA-Schedule.xlsx
ABCDEFGHIJKLMNOPQRST
6VISITORPhiladelphiaLA LakersOrlandoWashingtonHoustonNew OrleansChicagoClevelandNew YorkOklahoma CityCharlotteDenverDallasPortlandMilwaukeeLA ClippersNew OrleansSan AntonioChicago
7HOMEBostonGolden StateDetroitIndianaAtlantaBrooklynMiamiTorontoMemphisMinnesotaSan AntonioUtahPhoenixSacramentoPhiladelphiaL.A. LakersCharlotteIndianaWashington
8DATE10/18/2210/19/2210/20/2210/21/22
Sheet1
 
Upvote 0
If the dates are actually dates and not strings that look like dates this worked for me:

VBA Code:
Option Explicit
Sub Macro2()

    Dim cln As New Collection
    Dim i As Long
    Dim wsSrc As Worksheet
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("2022-2023")
    
    On Error Resume Next
        For i = 2 To wsSrc.Cells(3, Columns.Count).End(xlToLeft).Column
            If Len(wsSrc.Cells(3, i)) > 0 Then
                cln.Add CStr(Format(DateValue(wsSrc.Cells(3, i)), "DD-MMM-YYYY")), Format(DateValue(wsSrc.Cells(3, i)), "DD-MMM-YYYY")
                If Err.Number <> 0 Then
                    wsSrc.Cells(3, i).ClearContents
                End If
                Err.Clear
            End If
        Next i
    On Error GoTo 0
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
If the dates are actually dates and not strings that look like dates this worked for me:

VBA Code:
Option Explicit
Sub Macro2()

    Dim cln As New Collection
    Dim i As Long
    Dim wsSrc As Worksheet
   
    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("2022-2023")
   
    On Error Resume Next
        For i = 2 To wsSrc.Cells(3, Columns.Count).End(xlToLeft).Column
            If Len(wsSrc.Cells(3, i)) > 0 Then
                cln.Add CStr(Format(DateValue(wsSrc.Cells(3, i)), "DD-MMM-YYYY")), Format(DateValue(wsSrc.Cells(3, i)), "DD-MMM-YYYY")
                If Err.Number <> 0 Then
                    wsSrc.Cells(3, i).ClearContents
                End If
                Err.Clear
            End If
        Next i
    On Error GoTo 0
   
    Application.ScreenUpdating = True

End Sub
Perfect. Thank you.
 
Upvote 0
its deleting the entire column.
Yes, that's more what Excel "Remove Duplicates" does with rows so I did think that is what you wanted. :)
In future, giving the expected results as well as the original data would be a good idea to help clarify what you are after.

can it only clear.contents of the cell?
This is how I would do it.

VBA Code:
Sub Remove_Dupes_v2()
  Dim c As Long
  
  Application.ScreenUpdating = False
  For c = Cells(3, Columns.Count).End(xlToLeft).Column To 3 Step -1
    If Cells(3, c).Value = Cells(3, c - 1).Value Then Cells(3, c).ClearContents
  Next c
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Yes, that's more what Excel "Remove Duplicates" does with rows so I did think that is what you wanted. :)
In future, giving the expected results as well as the original data would be a good idea to help clarify what you are after.


This is how I would do it.

VBA Code:
Sub Remove_Dupes_v2()
  Dim c As Long
 
  Application.ScreenUpdating = False
  For c = Cells(3, Columns.Count).End(xlToLeft).Column To 3 Step -1
    If Cells(3, c).Value = Cells(3, c - 1).Value Then Cells(3, c).ClearContents
  Next c
  Application.ScreenUpdating = True
End Sub
Perfect. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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