VBA: Delete Columns if Duplicate Header

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
Hello,

I can't manage to get a macro working. I'd like to create a macro that removes all columns that have duplicate headers.

Please advise,

Steve
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
If you have 2 columns with a header "Fluff" do you want to delete both columns, or just 1?
If just 1 does it matter which?
 
Upvote 0
If the column headers are in row 1, this will remove duplicate columns:

Code:
Public Sub RemoveDuplicateColumns()

Dim lastCol As Long
Dim thisCol As Long

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
thisCol = 2
Do While thisCol <= lastCol
    If Application.Match(Cells(1, thisCol).Value, Range("1:1"), 0) < thisCol Then
        Cells(1, thisCol).EntireColumn.Delete xlShiftToLeft
        lastCol = lastCol - 1
    Else
        thisCol = thisCol + 1
    End If
Loop

End Sub

WBD
 
Upvote 0
Why doesn't this work?
Code:
Sub Remove_Duplicate_Columns()


Dim lastCol As Long
Dim thisCol As Long


    With Sheets("Data Sheet")
        lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        thisCol = 2
        Do While thisCol <= lastCol
            If Application.Match(Cells(1, thisCol).Value, Range("1:1"), 0) < thisCol Then
                Cells(1, thisCol).EntireColumn.Delete xlShiftToLeft
                lastCol = lastCol - 1
            Else
                thisCol = thisCol + 1
            End If
    
    Loop
End With
End Sub
 
Upvote 0
Try
Code:
Sub Remove_Duplicate_Columns()


Dim lastCol As Long
Dim thisCol As Long

   
   With Sheets("MA (2)")
   lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
   thisCol = 2
      Do While thisCol <= lastCol
         If Application.Match(.Cells(1, thisCol).Value, .Range("1:1"), 0) < thisCol Then
            .Cells(1, thisCol).EntireColumn.Delete xlShiftToLeft
            lastCol = lastCol - 1
         Else
            thisCol = thisCol + 1
         End If
      Loop
   End With
End Sub
 
Upvote 0
Or

Code:
Sub Remove_Duplicate_Columns()
    Dim lastCol As Long
    Dim thisCol As Long

    With Sheets("Data Sheet")
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        
        For thisCol = lastCol To 2 Step -1
            If Application.Match(.Cells(1, thisCol).Value, Range(.Cells(1, 1), .Cells(1, lastCol)), 0) <> thisCol Then
                .Cells(1, thisCol).EntireColumn.Delete xlShiftToLeft
            End If
        Next thisCol
    End With
End Sub

M.
 
Last edited:
Upvote 0
If you have 2 columns with a header "Fluff" do you want to delete both columns, or just 1?
If just 1 does it matter which?
Hi, may I know how to remove both columns or 3 columns with same header name?
 
Upvote 0
Please start a thread of your own for this question. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
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