Macro to delete columns (with headers) that are blank or contain a 0

saraapple

Board Regular
Joined
Feb 3, 2020
Messages
165
Office Version
  1. 2010
Platform
  1. Windows
I have a master sheet with 240 columns, every column has a title (cells A1,B1 etc.)
I need to delete the columns which are blank or contain 0 but my data is text, times and numbers.
All the macros I have found delete the text can anyone assist?
Thank you
Sara
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
VBA Code:
Sub saraapple()
   With Range("1:1")
      .Replace 0, "", xlWhole, , , , False, False
      On Error Resume Next
      .SpecialCells(xlBlanks).EntireColumn.Delete
      On Error GoTo 0
   End With
End Sub
 
Upvote 0
Thank you so much for the quick reply! I have tried the code and nothing happens - it runs fine but the data does not change.
I wondered if I needed to add it to the code I found that does not quite work but still cant get anything to work.
If it helps the code I found is:

Sub DeleteBlankColumns() 'Step1: Declare your variables.
Dim MyRange As Range
Dim iCounter As Long
'Step 2: Define the target Range.
Set MyRange = ActiveSheet.UsedRange

'Step 3: Start reverse looping through the range.
For iCounter = MyRange.Columns.Count To 1 Step -1

'Step 4: If entire column is empty then delete it.
If Application.CountA(Intersect(MyRange.Offset(3), Columns(iCounter))) = 0 Then
Columns(iCounter).Delete
End If
'Step 5: Increment the counter down
Next iCounter

End Sub

THIS IS NOT MY CODE - but it does work fine deleting the columns which are blank but also that contain text.
I was thinking perhaps amending this code to recognise text (so not delete) and also see a "0" as a blank so delete also.

Thank you as always
 
Upvote 0
I misunderstood, I thought you wanted to delete the column if the header was blank or 0.
That code should not delete columns that contain text
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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