VBA: Delete Column if Only Header

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
Hi,

I have this great macro that removes all columns that ONLY contain a header. However, it doesn't seem to be working anymore. Sometimes also, it doesn't remove all of the columns that I thought it would.

I also have a macro set up that looks for empty cells and clears them if they are blank, but maybe thats not working.

But anyway, can someone take a look at this macro and tell me why it isn't removing columns that only contain headers?

Code:
Sub Delete_Row_If_Only_Header()    Dim col As Long
    Application.ScreenUpdating = False
    For col = 42 To 1 Step -1
        If Application.CountA(Columns(col)) = 1 Then Columns(col).Delete
    Next col
    Application.ScreenUpdating = True
End Sub
 
When you look in a column that should have been deleted but was not what if any thing is in the column? If you go to the last row in the column and do Ctrl + up arrow does it jump to the header?
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
LouiseH

Is there a way I can have "" counted as a null value, or is there a way for me to remove "" altogether?

Hope you are well,

Steve
 
Upvote 0
LouiseH

Is there a way I can have "" counted as a null value, or is there a way for me to remove "" altogether?

Hope you are well,

Steve

Are you still looking for that macro or are you talking about Something else ?
I sent you this code which worked for me, it doesn't count the "" values :

Code:
Sub Delete_Row_If_Only_Header()

    Dim col As Long
    Application.ScreenUpdating = False
    For col = Columns.Count To 1 Step -1
        lr = ActiveSheet.Cells(ActiveSheet.Rows.Count, col).End(xlUp).Row
        If Evaluate("SUMPRODUCT((LEN(" & Range(Cells(1, col), Cells(lr, col)).Address & ") > 0)*1)") = 1 Then Columns(col).Delete
    Next col
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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