0
votes

I have the following VBA code that will delete columns that are entirely blank. I would like to modify the code so that if a range below the first three rows (headers) is blank, then the entire column will be deleted. Can anyone assist please?

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(Columns(iCounter).EntireColumn) = 0 Then
       Columns(iCounter).Delete
       End If
'Step 5: Increment the counter down
    Next iCounter
End Sub
1
Perhaps If Application.CountA(Columns(iCounter).EntireColumn) = 3?SJR

1 Answers

1
votes

Try:

If Cells(Rows.Count, iCounter).End(xlUp).Row<=3 Then
  Columns(iCounter).Delete
End If

"Cells(Rows.Count, iCounter).End(xlUp).Row" finds the row # of the last filled cell in the column. If that row is one of the top 3, then we know that all cells below it are empty