VBA Delete Blank Rows Including First Row

klvn88

New Member
Joined
Sep 22, 2015
Messages
21
I'm referencing a code that was posted here: https://www.mrexcel.com/forum/excel...lete-blank-rows-formula-but-has-no-value.html

Code:
Sub deleteBlankRows()


Dim lastRow As Long, lastCol As Long, i As Long, myBlanks As Long


With Sheets("Sheet1")
    lastRow = .UsedRange.Rows(.Rows.Count).End(xlUp).Row
    lastCol = .UsedRange.Columns(.Columns.Count).End(xlToLeft).Column
    For i = lastRow To 2 Step -1
        myBlanks = Application.WorksheetFunction.CountBlank(.Range(.Cells(i, 1), .Cells(i, lastCol)))
        If myBlanks = lastCol Then
            .Rows(i).Delete
        End If
    Next i
End With


End Sub

This code doesn't appear to run properly if the first row is blank. I think it has something to do with UsedRange. Can someone please tell me where I can address this in the code?

Thank you!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
maybe change "For i = lastRow To 2" to a 1?

Code:
Sub deleteBlankRows()


Dim lastRow As Long, lastCol As Long, i As Long, myBlanks As Long


With Sheets("Sheet1")
    lastRow = .UsedRange.Rows(.Rows.Count).End(xlUp).Row
    lastCol = .UsedRange.Columns(.Columns.Count).End(xlToLeft).Column
    For i = lastRow To 1 Step -1
        myBlanks = Application.WorksheetFunction.CountBlank(.Range(.Cells(i, 1), .Cells(i, lastCol)))
        If myBlanks = lastCol Then
            .Rows(i).Delete
        End If
    Next i
End With


End Sub
 
Upvote 0
Try changing this:

Code:
    lastRow = .UsedRange.Rows(.Rows.Count).End(xlUp).Row
    lastCol = .UsedRange.Columns(.Columns.Count).End(xlToLeft).Column

to this:

Code:
lastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 
Upvote 0
use usedrange instead of rows.count



Code:
Sub deleteBlankRows()
    Dim lastRow As Long, lastCol As Long, i As Long, myBlanks As Long
    With Sheets("Sheet1")
        lastRow = .UsedRange.Rows([COLOR=#0000ff].UsedRange.Rows.Count[/COLOR]).Row
        lastCol = .UsedRange.Columns([COLOR=#0000ff].UsedRange.Columns.Count[/COLOR]).Column
        For i = lastRow To 2 Step -1
            myBlanks = Application.WorksheetFunction.CountBlank(.Range(.Cells(i, 1), .Cells(i, lastCol)))
            If myBlanks = lastCol Then
                .Rows(i).Delete
            End If
        Next i
    End With
End Sub
 
Upvote 0
This code doesn't appear to run properly if the first row is blank. I think it has something to do with UsedRange. Can someone please tell me where I can address this in the code?

Thank you!


If the first row is empty, then the used range starts in row 2, hence you want to jump to the row according to the row number of the sheet (.rows.count), then this over passes the number of rows in the sheet , that's why it sends the error.
 
Upvote 0
The failure to delete the 1st row is simply (as fhqwgads has posted) that i (which is the row number) can only go to row 2 because of
Code:
For i = lastRow To [COLOR="#FF0000"]2[/COLOR] Step -1
.

The usedrange is only being used to determine the last row and last column. Admittedly I wouldn't use it to determine either because of the issues with usedrange, but it is not causing the issue because it isn't being used to determine the start row.

Personally I also wouldn't use COUNTBLANK, I would prefer using WorksheetFunction.Counta = 0.
 
Last edited:
Upvote 0
I didn't expect so many responses! The first response changing the 2 to a 1 resolved the issue. Thank you to all!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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