Help with VBA Code Loop

johnbird1988

Board Regular
Joined
Oct 6, 2009
Messages
199
This piece of VBA code is looping through a range of around 200 rows but for some reason it keeps looping even after all rows have been addressed. I’m not sure if the reason it is looping so much is because of the part highlighted. Can someone explain what “Last To 1 Step -1” does and how I can get this to only loop through a range of circa 200 rows?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
<o:p> </o:p>
Last = Cells(Rows.Count, "AC").End(xlUp).Row<o:p></o:p>
For i = Last To 1 Step -1<o:p></o:p>
If (Cells(i, "AC").Value) = "N" Then<o:p></o:p>
Cells(i, "A").EntireRow.Delete<o:p></o:p>
End If<o:p></o:p>
Next i<o:p></o:p>
<o:p> </o:p>
Last = Cells(Rows.Count, "AB").End(xlUp).Row<o:p></o:p>
For i = Last To 1 Step -1<o:p></o:p>
If (Cells(i, "AB").Value) = "Yes" Then<o:p></o:p>
Cells(i, "A").EntireRow.Delete<o:p></o:p>
End If<o:p></o:p>
Next i<o:p></o:p>

Thank you
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I’m not sure if the reason it is looping so much is because of the part highlighted.
I couldn't see anything highlighted - all the code looked uniform.

Code:
Last = Cells(Rows.Count, "AC").End(xlUp).Row<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
This attempts to find the last cell in column AC which contains any sort of value.

Code:
For i = Last To 1 Step -1
This creates a loop which starts at the cell identified in the previous step and loops upwards until it gets to the cell in row 1 of column AC. (The code which follows it deletes any row where there's an "N" in the cell.)

A good start would be to find out where it thinks the last cell is. Insert this before each of the FOR statements:-
Code:
MsgBox "Last cell is in row " & Last

Now run the macro again. Two messages boxes will appear during execution. What are they telling you? What were you expecting them to say?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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