How to Delete Duplicate Column with Same Header Name

Sherli

New Member
Joined
Apr 27, 2020
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have face same issue while delete duplicate column with same header name. I have 1000 + column in my excel file and there are many duplicate column with same header name, may I know how to delete the duplicate column?

Please advise,
Sherli
 

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.
Do you want to keep the first instance of each column?
Also where are your headers?
 
Upvote 0
VBA Code:
Sub t()
Dim i As Long
    For i = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
        If Application.CountIf(Rows(1), Cells(1, i).Value) > 1 Then
            Columns(i).Delete
        End If
    Next
End Sub
Test this on a copy of your file first.
 
Last edited:
Upvote 0
VBA Code:
Sub t()
Dim i As Long
    For i = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
        If Application.CountIf(Rows(1), Cells(1, i).Value) > 1 Then
            Columns(i).Delete
        End If
    Next
End Sub
Test this on a copy of your file first.
I have try this and it does not work.
 
Upvote 0
In what didn't that code work?
 
Upvote 0
In what didn't that code work?
After I run this, it do not remove all duplicate header. It still remain the 1st column of that duplicate header.

Example:
Original file:
HappySadHappySadFunnySadJoy

Result I get when I run the code:
HappySadFunnyJoy

Result I want:
FunnyJoy
 
Last edited:
Upvote 0
Ok, how about
VBA Code:
Sub Sherli()
   Dim Cl As Range, Rng As Range
   
   Set Rng = Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl
         Else
            Set Rng = Union(Rng, Cl, .Item(Cl.Value))
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub Sherli()
   Dim Cl As Range, Rng As Range
  
   Set Rng = Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl
         Else
            Set Rng = Union(Rng, Cl, .Item(Cl.Value))
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
Thanks, Fluff. The code is work nicely.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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