For Loop exiting early

Chopper50

New Member
Joined
Mar 14, 2013
Messages
30
I have For/ Next loop where I use i=2 to Lastrow. In the loop I insert rows and increment the Lastrow variable to account for the additional row I inserted.

It appears the the program only evaluates the For statement against the original Lastrow value. Is this the case? Is there anything to be done that would allow the Lastrow variable to be incremented and used?

I've seen where the suggestion is to start from the bottom and decrement the i value. Is this the only way?

Chad
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I've seen where the suggestion is to start from the bottom and decrement the i value. Is this the only way?
Whilst it's not the only way, it is strongly recommended that you take this approach.
 
Upvote 0
It appears the the program only evaluates the For statement against the original Lastrow value. Is this the case?
Yes... the limits for a For..Next loop are only evaluated once at the time the loop starts and are retained until the loop ends.



Is there anything to be done that would allow the Lastrow variable to be incremented and used?
Use a Do..Loop and handle incrementing the variable manually within it.



I've seen where the suggestion is to start from the bottom and decrement the i value. Is this the only way?
As Fluff said, it is the recommended method as it is the simplest way to do this.
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,242
Members
453,026
Latest member
cknader

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