Delete entire column if row 5 is blank

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi,

I have a file where i'd like to delete entire column where row #5 is blank. I have below code where it deletes entire column if it is completely blank.

I want to narrow it down and delete entire column if row 5 is blank in the range. Can anyone help me modify the code. My range is from column A through column BM. Thanks

VBA Code:
Sub DeleteBlankColumns()

    Dim MyRange As Range
    Dim iCounter As Long

    Set MyRange = ActiveSheet.UsedRange
    
    For iCounter = MyRange.Columns.Count To 1 Step -1
    
       If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
       Columns(iCounter).Delete
       End If

    Next iCounter
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I modified the code and below worked for me.

VBA Code:
Sub DeleteBlankrow5blank()
Application.ScreenUpdating = False

    Dim MyRange As Range
    Dim iCounter As Long

    Set MyRange = ActiveSheet.UsedRange
                
    For iCounter = MyRange.Columns.Count To 1 Step -1
    
    Set rng = MyRange.Cells(5, iCounter)
    
       If rng.Value = 99 Then
        
       Columns(iCounter).Delete
 
      
              End If

    Next iCounter
    
 
 Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
If the cells are truly blank then this should work

VBA Code:
Sub DeleteColumns_v1()
  On Error Resume Next
  Range("A5:BM5").SpecialCells(xlBlanks).EntireColumn.Delete
  On Error GoTo 0
End Sub

Or if the cells might contain (or have contained) "" and the other values are not formula results where the formula needs to be retained, then try

VBA Code:
Sub DeleteColumns_v2()
  With Range("A5:BM5")
    .Value = Evaluate(Replace("if(#="""",TRUE,#)", "#", .Address))
    On Error Resume Next
    .SpecialCells(2, 4).EntireColumn.Delete
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
If the cells are truly blank then this should work

VBA Code:
Sub DeleteColumns_v1()
  On Error Resume Next
  Range("A5:BM5").SpecialCells(xlBlanks).EntireColumn.Delete
  On Error GoTo 0
End Sub

Or if the cells might contain (or have contained) "" and the other values are not formula results where the formula needs to be retained, then try

VBA Code:
Sub DeleteColumns_v2()
  With Range("A5:BM5")
    .Value = Evaluate(Replace("if(#="""",TRUE,#)", "#", .Address))
    On Error Resume Next
    .SpecialCells(2, 4).EntireColumn.Delete
    On Error GoTo 0
  End With
End Sub
Thanks
 
Upvote 0
If the cells are truly blank then this should work

VBA Code:
Sub DeleteColumns_v1()
  On Error Resume Next
  Range("A5:BM5").SpecialCells(xlBlanks).EntireColumn.Delete
  On Error GoTo 0
End Sub

Or if the cells might contain (or have contained) "" and the other values are not formula results where the formula needs to be retained, then try

VBA Code:
Sub DeleteColumns_v2()
  With Range("A5:BM5")
    .Value = Evaluate(Replace("if(#="""",TRUE,#)", "#", .Address))
    On Error Resume Next
    .SpecialCells(2, 4).EntireColumn.Delete
    On Error GoTo 0
  End With
End Sub
I'd like to run this code in two separate worksheets. One name Product and other name POS. How can i run it simultaneously?
 
Upvote 0
I'd like to run this code ..
Which code?
You posted a code and I posted two of them so there are three to choose from. Which one does what you want the best?
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,297
Members
449,218
Latest member
Excel Master

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