Search This Blog

Sunday, August 15, 2010

How to delete unwanted columns via macro for data analysis


1. Place 'X' in Row 2 in whichever columns you would like to delete.

2. Paste the following macro in Module.
(Insert > Module)


Sub del_columns()
Dim ws As Worksheet
Dim lastcolm As Long
Dim i As Long
For Each ws In Worksheets
With ws
lastcolm = .Cells(2, .Cells.Columns.Count).End(xlToLeft).Column
For i = lastcolm To 1 Step -1
If UCase(.Cells(2, i)) = "X" Then
.Columns(i).Delete
End If
Next i
End With
Next ws
End Sub

3. Press Alt+F8.

4. Click Run .