Delete Empty Columns From A Given Range In A worksheet With VBA

This VBA Code allows you to delete the empty column which from a given range

You can use the code in Excel for mac & windows

  1. Copy the below VBA Code by clicking on the copy box
  2. Open Microsoft Visual Basic for Applications window Developer Ribbon Tab > Visual Basic (Shortcut keys as follows for mac & windows)
    1. mac users fn  + ⌥ [option] + F11 
    2. windows users Alt  + F11
  3. From the menu bar select Insert > Module and insert new Modules
  4. Paste the code into the module.

Note: It is always to take a back-up copy of your original before running the code on original file.

Sub DeleteEmptyColumns()
  'Made It Easy By ExcelExciting.com
  Dim rng As Range
  Dim SelectRng As Range
  title = "ExcelExciting.com"
  Set SelectRng = Application.Selection
  Set SelectRng = Application.InputBox("Range :", title, SelectRng.Address, Type:=8)
  Application.ScreenUpdating = False
  For i = SelectRng.Columns.Count To 1 Step -1
      Set rng = SelectRng.Cells(1, i).EntireColumn
      If Application.WorksheetFunction.CountA(rng) = 0 Then
          rng.Delete
      End If
  Next
  Application.ScreenUpdating = True
End Sub

From the ribbion select Developer > Macros > Run

Delete-Empty-Columns
1 5 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x