How to install your new code
- Copy the Excel VBA code
- Select the workbook in which you want to store the Excel VBA code
- Press Alt+F11 to open the Visual Basic Editor
- Choose Insert > Module
- Edit > Paste the macro into the module that appeared
- Close the VBEditor
- Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)
Sub RemoveDuplicateColumn()
Dim iCl As Integer, rFound As Range
iCl = Cells(1, Columns.Count).End(xlToLeft).Column
Application.ScreenUpdating = False
Rows("1:1").Insert
With Range("B1", Cells(1, iCl))
.Formula = "=IF(COUNTIF(R2C1:R[1]C,R[1]C)>1,""Delete"",""Retain"")"
.Value = .Value
Set rFound = .Find("Delete")
Do While Not rFound Is Nothing
rFound.EntireColumn.Delete
Set rFound = .Find("Delete")
Loop
End With
Rows("1:1").Delete
Application.ScreenUpdating = True
End Sub
To run the Excel VBA code:- Press Alt-F8 to open the macro list
- Select a macro in the list
- Click the Run button
Bookmarks