Delete column if row is blank

rozak

New Member
Joined
Nov 24, 2016
Messages
8
Leave rows 1-4 untouched for the header. Then I want to use row 5 to mark the columns to keep i.e. if row 5 is empty, delete that column from row 5 down. I'm getting an "Expected...Array" message for some reason. Thanks in advance.


VBA Code:
Sub DeleteBlankColumns()        'delete columns if row 5 is blank
 
  Dim myLastCol As Long         '
  Dim myLastRow As Long
  Dim myCol As Long
 
  myLastCol = Cells(5, Columns.Count).End(xlToLeft).Column
 
  For myCol = myLastCol To 1 Step -1

      myLastRow = Cells(Rows.Count, myCol).End(xlUp).Row
      If myLastRow(Cells(5, myLastRow).Value, "") Then
      Columns(myLastRow).Delete Shift:=xlToLeft
      End If
      
  Next myCol
 
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Are you trying to delete the entire column (including the header, or just clear the cells below the header?
 
Upvote 0
VBA Code:
Sub DeleteBlankColumns()
Dim a%
With Rows(5).SpecialCells(xlCellTypeBlanks)
    For a = .Areas.Count To 1 Step -1
        Intersect(ActiveSheet.UsedRange, .Areas(a).Resize(Rows.Count - 4)).Delete
    Next
End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub rozak()
   Intersect(Rows("5:1048576"), Rows(5).SpecialCells(xlBlanks).EntireColumn).Value = ""
End Sub
 
Upvote 0
Fluff you gave me exactly what I asked for (sorry, I wasn't clear). Footoo read my mind and gave me what I wanted. Thank you both. Much appreciated.
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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