Why does this macro skip the last row?

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This macro is supposed to delete any blank rows, it does so except for the row above the last row.
The macro is:

Sub delrow()
Set rang = ActiveSheet.UsedRange
For i = rang.Rows.Count To 2 Step -1
If Cells(i, 2) = "" Then
Rows(i).Delete
End If
Next i
End Sub

A quick check through the Immediate window confirms that the all the rows are picked up in the UsedRange property, the rang.Rows.Count however returns row 17 rather than row 19. There is a difference in how the macro performs if the Jan title is located in cell B2 where it works fine, if the data is moved down a row so that the Jan title is in B3 then it doesn't pick up the last row.

JanFebMar
100200300
110220330
120240360
130260390
140280420
150300450
160320480
170340510
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Maybe this way
VBA Code:
Sub delrow()
Dim lr As Long, i As Long
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
For i = lr To 2 Step -1
If Cells(i, 2) = "" Then
Rows(i).Delete
End If
Next i
End Sub
 
Upvote 0
First: When posting code, please use code tags so that the code is formatted in an easier-to-read format.

If there is nothing in 1 or more rows at the top of the sheet, then UsedRange may not include those rows. In that case rang.Rows.Count is not the same as finding the last used row in the sheet & hence your code couls miss 1 (or more) rows.
Michael has suggested a safer way to find the last row.

Another way is to delete all the rows at once.
If the heading is in, say B3

VBA Code:
Sub Del_Rows()
  On Error Resume Next
  Range("B3", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0
Hi Michael,

Yes, that works well. I wonder is it an inherent failing of the Rows.Count property or is there something else wrong with macro? It's a really simple macro so there can't be much that could have gone wrong.
 
Upvote 0
Thanks Gentlemen for clearing that up. Sorry about the use of the code tags, I'll rectify that next time.
This one really had me puzzled because the correct UsedRange address was being returned so in theory the Rows.Count should have worked. Michael, I like the safer way of ensuring that it is picked up and Peter I love the economy of your coding, one line to do all that work and I thought mine was brief.
 
Upvote 0
Glad WE helped out....and yeah, @Peter_SSs is very adept at providing REALLY efficient code !!....?
 
Upvote 0
the correct UsedRange address was being returned
Was being returned where as nothing in your posted code is returning an address?

What was the exact UsedRange address returned?
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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