Delete Empty Columns

Add to Favorites

This macro will delete columns which are completely empty. This means that if there is no data within the entire column it will be removed. This macro works on a selection of cells within the excel spreadsheet. That means that you must select cells and then run the macro for it to do anything.

Note: The first macro will work for Excel 2007 whereas the second macro will work for Previous Excel Versions. This is due to the change in the number of rows from previous excel versions to Excel 2007.

Where to install the macro:  Module

Delete Empty Columns (Excel 2007 and Beyond)

Sub Delete_Empty_Columns()

first = Selection.Column
last = Selection.Columns(Selection.Columns.Count).Column

 For i = last To first Step -1

  If WorksheetFunction.CountBlank(ActiveSheet.Columns(i)) = 1048576 Then
   Columns(i).Delete
  End If

 Next i
End Sub

Delete Empty Columns (Previous Versions of Excel)

Sub Delete_Empty_Columns()

first = Selection.Column
last = Selection.Columns(Selection.Columns.Count).Column

 For i = last To first Step -1

   If WorksheetFunction.CountBlank(ActiveSheet.Columns(i)) = 65536 Then
   Columns(i).Delete
  End If

Next i
End Sub





Similar Content on TeachExcel
Delete Empty or 'Broken' Named Ranges (#REF!)
Macro: This macro will delete named ranges which refer to empty or #REF! ranges. This means that ...
Delete All Empty Rows or Blank Cells from a Range in Excel
Tutorial: How to quickly delete all empty cells or rows from a range in Excel.  This allows you to q...
Delete Rows in Excel if Completely Empty
Macro: This macro will delete only completely blank rows in an excel spreadsheet. It allows you t...
Delete Blank Rows in Excel
Macro: This is a macro which will delete blank rows in excel. This version will delete an entire ...
Delete Duplicate Values in All Versions of Excel
Tutorial: How to delete duplicate values from a data set in all versions of Excel.  This includes Ex...
Delete Duplicate Rows
Macro: This macro will delete rows that appear twice in a list or worksheet. If two cells are...



How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.


Similar Content
Delete Empty or 'Broken' Named Ranges (#REF!)
Macro: This macro will delete named ranges which refer to empty or #REF! ranges. This means that ...
Delete All Empty Rows or Blank Cells from a Range in Excel
Tutorial: How to quickly delete all empty cells or rows from a range in Excel.  This allows you to q...
Delete Rows in Excel if Completely Empty
Macro: This macro will delete only completely blank rows in an excel spreadsheet. It allows you t...
Excel Forum