Delete empty columns that have labels in row 1.

jskasango

Board Regular
Joined
Jul 18, 2012
Messages
203
Office Version
  1. 365
Platform
  1. Windows
I'm using Excel 2016 Professional and I need help with deleting completely blank columns from my active worksheet. Row 1 contains labels.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I'd use a simple VBA macro for this. Press Alt+F11 to open Visual Basic Editor. In menu: Insert -> Module
in a window which opens paste the code:
VBA Code:
Sub empty_cols_remover()
Dim lc As Long, i As Long, counter As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For i = lc To 1 Step -1
  counter = WorksheetFunction.CountA(Range(Cells(2, i), Cells(Rows.Count, i)))
  If counter = 0 Then Cells(1, i).EntireColumn.Delete shift:=xlToLeft
Next i
End Sub

Close VBE (this will take you back to your workbook), press Alt+F8 and run the macro (it willl be on a list)
the result shall be as presented below:

Before:

żarówki - solver.xlsx
ABCDEFGHIJKLMNO
1Nagłowek1Nagłowek2Nagłowek3Nagłowek4Nagłowek5Nagłowek6Nagłowek7Nagłowek8Nagłowek9Nagłowek10Nagłowek11Nagłowek12Nagłowek13
21
3
4
5
6aaaaaa
7
8
9
10
11
12
13
14
15
Sheet2
Cell Formulas
RangeFormula
F6F6=C6


After:

żarówki - solver.xlsx
ABCDEFGHIJ
1Nagłowek3Nagłowek6Nagłowek9
21
3
4
5
6aaaaaa
7
Sheet2
Cell Formulas
RangeFormula
B6B6=A6


BTW. If it is just "single or rare action", you may ignore warnings and save the file as not macro-enabled format. the code will not be there when you reopen the file. But if you plan to reuse the code, save the workbook in macro-enabled format like xlsm or xlsb. Then next time make worksheet with table active then Alt+F8 and run the macro.,
 
Upvote 0
Here is another macro for you to consider:

VBA Code:
Sub Delete_Columns()
  Dim j As Long
  For j = Cells(1, Columns.Count).End(1).Column To 1 Step -1
    If Cells(Rows.Count, j).End(3).Row = 1 Then Columns(j).Delete
  Next
End Sub
 
Upvote 0
Minor (rather no practical meaning) problem with
VBA Code:
Cells(Rows.Count, j).End(3).Row = 1
code is that it is true in case of both empty column, and really full column in case of current excel formats rows 1 to
1048576
But of course similar applies to all columns - if we have headers in A1:XFD1 both above codes will fail.
 
Upvote 0

Forum statistics

Threads
1,226,006
Messages
6,188,358
Members
453,471
Latest member
D Tyme

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top