Delete Blank Columns

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Evening All

Is there a possible shorter way to delete Columns with No Data.

Thanks

Code:
Sub DeleteBlankCol()
    col = Cells(1, Columns.Count).End(xlToLeft).Column
    For c = col To 1 Step -1
    If WorksheetFunction.CountA(Columns(c)) = 0 Then
    Columns(c).Delete
    End If
    Next
    End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't know about shorter (or why the code length would be an issue :confused:) but the below is probably faster (untested).
Assumes there is data in column A.

Code:
[color=darkblue]Sub[/color] RemoveBlankColumns()
    [color=darkblue]Dim[/color] myRng [color=darkblue]As[/color] Range, RngDel [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] ColCount [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] x [color=darkblue]As[/color] [color=darkblue]Long[/color]

    Application.ScreenUpdating = [color=darkblue]False[/color]

    [color=darkblue]Set[/color] myRng = ActiveSheet.UsedRange
    ColCount = myRng.Columns.Count

    [color=darkblue]For[/color] x = ColCount [color=darkblue]To[/color] 1 [color=darkblue]Step[/color] -1
        [color=darkblue]If[/color] Application.WorksheetFunction.CountA(myRng.Columns(x)) = 0 [color=darkblue]Then[/color]
            [color=darkblue]If[/color] RngDel [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color] [color=darkblue]Set[/color] RngDel = myRng.Columns(x)
            [color=darkblue]Set[/color] RngDel = Union(RngDel, myRng.Columns(x))
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] x

    [color=darkblue]If[/color] [color=darkblue]Not[/color] RngDel [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        RngDel.EntireColumn.Delete
    [color=darkblue]End[/color] [color=darkblue]If[/color]

    Application.ScreenUpdating = [color=darkblue]True[/color]

[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0
Evening All

Is there a possible shorter way to delete Columns with No Data.

Thanks

Code:
Sub DeleteBlankCol()
    col = Cells(1, Columns.Count).End(xlToLeft).Column
    For c = col To 1 Step -1
    If WorksheetFunction.CountA(Columns(c)) = 0 Then
    Columns(c).Delete
    End If
    Next
    End Sub
Well, here is one way you could "shorten" it :diablo:
Code:
Sub DeleteBlankCol()
   For c = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
     If WorksheetFunction.CountA(Columns(c)) = 0 Then Columns(c).Delete
   Next
 End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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