Delete columns if they have same header

chouba

New Member
Joined
Apr 20, 2021
Messages
9
Hello,
I m working on VBA code to delete columns if they have same headers. I have found some solution but most of them takes too much time as they read cells 1 by 1.
Here are some information :
- My headers are in row 1
- I need an efficient vba code that can handle 10 000+ columns in fastest way( maybe work with a variable that contains the range and than paste it back on the sheet)
- I want to keep the first instance of each column ( for example if we have headers like = " pizza - pizza - burger" I want to keep "pizza - burger" columns)
- I cannot provide an exemple but if more informations are needed I can answer you

Thanks a lot for your help :)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
try

VBA Code:
Sub chouba()
Dim wsh As Worksheet
Dim A As Range

For Each wsh In ActiveWorkbook.Worksheets
    Do
        Set A = Rows(1).Find(What:="Pizza", LookIn:=xlValues, lookat:=xlPart)
        If A Is Nothing Then Exit Do
        A.EntireColumn.Delete
    Loop
Next wsh

End Sub
 
Upvote 0
Thank you for your reply !

Unfortunately I dont want to remove columns with a specific name but all the columns that are duplicated based on header name.
 
Upvote 0
How about
VBA Code:
Sub chouba()
   Dim Cl As Range, Rng As Range
   
   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, Nothing
         Else
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
 
Upvote 0
Solution
Ok it works perfect thanks alot! I tried with 10k columns and woks great !

Last question about my code structure :

For my data I need first to get the headers from a Vlookup (in another sheet). In order to get this faster i do it in a variant like this then I use your code
Do you think there is an more effcient way to do it ?

VBA Code:
'Store the data in a range
data = Range("A1", Cells(lastRow, lastCol)).Value

'Vlookup to get headers
For i = 2 To lastCol - 1
    data(1, i) = Application.WorksheetFunction.VLookup(ActiveSheet.Cells(RowIndex:=1, ColumnIndex:=i + 1), Range("Dataa"), 21, 0)
Next i

'Put back on the sheet
Range("A1", Cells(lastRow, lastCol)).Value = data

'Here i put your piece of code

'Here I do same steps that before but for rows ( with Cells.RemoveDuplicates Columns:=Array(1) which is quite fast)


Thanks again for your time I m still discovering VBA coding :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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