Delete columns macro

thecueball

Board Regular
Joined
Nov 11, 2004
Messages
66
Hi,

I use the following macro to delete any blank rows in my worksheets, can it be changed to use it for deleteing any blank columns within my data range???

If it can't, has anyone got a macro that can do the above?

TIA

Sub DeleteRowsIfZero()



FinalRow = Range("D65536").End(xlUp).Row

For x = FinalRow To 1 Step -1

If WorksheetFunction.CountA(Range(Cells(x, "A"), Cells(x, "F"))) = 0 Then

Rows(x).Delete Shift:=xlUp

End If

Next x
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub DeleteColsIfZero()

    Finalcol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    For x = Finalcol To 1 Step -1
    
        If WorksheetFunction.CountA(Columns(x)) = 0 Then
        
            Columns(x).Delete Shift:=xlToLeft
        End If
    Next x
End Sub
 
Upvote 0
Hi xld,

Thanks for the help, but when I run the macro, nothing happens...

I am in cell A1 if that makes any difference..

and the first column with no data is F, but the data in G does not move accross when I run the macro..

Does that make sense, or have I asked for the worng thing?!?!?!

Thanks!
 
Upvote 0
It is looking for row 1 for the headers. Is that your case?

The columns I want to keep and bunch together (i.e. delete the columns inbetween) all start at row 3, with the exception of 1 in column A, which starts in row 5

Thanks!
 
Upvote 0
Code:
Sub DeleteColsIfZero()

    Finalcol = Cells(3, Columns.Count).End(xlToLeft).Column
    
    For x = Finalcol To 1 Step -1
    
        If WorksheetFunction.CountA(Columns(x)) = 0 Then
        
            Columns(x).Delete Shift:=xlToLeft
        End If
    Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,436
Messages
6,078,546
Members
446,346
Latest member
shinbum

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