Macro to delete Duplicate Collumns

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have headings in row1 in sheet1


I would lie a macro to delete duplicate headings i.e only retain one unique header


For eg if "branch" appears in A1 and D1 , then only delete the second duplicate i.e Col D, eg Surname is in B1 and E1, the only delete col E etc


your assistance is most appreciated
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
Code:
Sub Howard1()
   Dim Rng As Range
   Dim i As Long
   
   With CreateObject("scripting.dictionary")
      For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
         If Not .Exists(Cells(1, i).Value) Then
            .Add Cells(1, i).Value, Nothing
         Else
            If Rng Is Nothing Then Set Rng = Columns(i) Else Set Rng = Union(Rng, Columns(i))
         End If
      Next i
   End With
   If Not Rng Is Nothing Then Rng.Delete
End Sub
 
Upvote 0
Thanks for the help, Code works perfectly


When is With CreateObject("scripting.dictionary") used in code and what is its function ?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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