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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,714
Members
448,294
Latest member
jmjmjmjmjmjm

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