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
 
Then as I explained before Rows.Count on this range gives 17, not 19. Therefore your loop checking each row will start at row 17 not row 19 & hence row 18 (or 19) never get checked.
Thanks Peter, good explanation.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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