Delete Blank Columns From The Excel
Following VBA Code will delete blank columns by ignoring 1st row (i.e header) in the excel
Steps to follow
1. Open New Excel Workbook
2. Press Alt+F11
3 Click Insert-->Modue
4. Copy and paste the below code in the Blank module inserted from above step 3
5. Click on the Save button or press Ctrl+S
6. Save the excel workbook in ".xlsm" OR "Macro-Enabled Workbook" format.
How to Use this Macro-Enabled WorkBook
1. Open the Macro-Enabled Workbook (eg: "delete_column.xlsm")
2. Copy-paste the data (including header) which contains blank columns.
Note: If your data doesn't have a header row then replace the following line of code
If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) =0 Then
3. Navigate to View in the Menu Bar
4. Select drop down from the Macros Icon--> View Macros
5. Ensure macro name "DeleteROPBlanks" is selected and macros in selected as (eg: "delete_column.xlsm")
6. Hit Run button
7. You can observe the blank columns are deleted.
Sub DeleteROPBlanks()
'
' DeleteROPBlanks Macro
Dim Col As Long, ColCnt As Long, Rng As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error GoTo Exits:
If Selection.Columns.Count > 1 Then
Set Rng = Selection
Else
Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column()))
End If
ColCnt = 0
For Col = Rng.Columns.Count To 2 Step -1
If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) < 2 Then
Rng.Columns(Col).EntireColumn.Delete
ColCnt = ColCnt + 1
End If
Next Col
Exits:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Comments
Post a Comment