Deleting empty columns

mhenk

Well-known Member
Joined
Jun 13, 2005
Messages
591
I'm getting too frustrated w/google & searching, so could someone please post some code to help me delete the empty columns in a sheet?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi mhenk

Please test the following code:

Code:
Sub DelEmptyCols()
Dim lLastCol As Long, lCol As Long

With Worksheets("Sheet1")
    lLastCol = .Cells.SpecialCells(xlCellTypeLastCell).Column
    For lCol = lLastCol To .UsedRange.Column Step -1
        If Application.WorksheetFunction.CountA(.Columns(lCol)) = 0 Then .Columns(lCol).Delete
    Next
End With
End Sub

Hope this is what you need
PGC
 

mhenk

Well-known Member
Joined
Jun 13, 2005
Messages
591
I'll try it, but it doesn't look like what I need. It looks like it will delete all the columns to the right of any data, which would be fine, but I (may) have intermediate columns that would need to be deleted.

EX: A, B, C, E, G may have data, I'd want to delete D and G.
 

mhenk

Well-known Member
Joined
Jun 13, 2005
Messages
591
Ah, NM, wasn't reading close enough. This seems to be working, i'll need to run a few more tests, and I'll let you know.
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi again

Well it should delete any empty column in the worksheet.

Please do a simple test after using the code:

Example. Column D should have been deleted but was not. In A1 write

=COUNTA(D:D)

If the column is really empty the result is 0.

Can you please do this simple test?
PGC
 

Forum statistics

Threads
1,141,043
Messages
5,703,917
Members
421,321
Latest member
blusky4

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
Top