Delete Empty Columns

Add to Favorites
Author:

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





Excel VBA Course

(35% Sale Ends Jan. 26)

Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course (35% Discount)

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 ...
Special Cell Magic - Finding/Deleting Empty Rows, Working with Filtered Data & More
: How to use SpecialCells to get sub-sets of ranges/cells in the worksheet and then do more ...
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 ...
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 Entire Rows Based on Predefined Criteria (Text)
Macro: This macro will allow you to specify certain criteria and then to delete rows based up...


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.

Tutorial Details
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 ...
Special Cell Magic - Finding/Deleting Empty Rows, Working with Filtered Data & More
: How to use SpecialCells to get sub-sets of ranges/cells in the worksheet and then do more ...
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 ...
Excel VBA Course

(35% Sale Ends Jan. 26)

Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course (35% Discount)