For-Next loop closing point not updating...

lopiteaux

Board Regular
Joined
Jun 8, 2011
Messages
77
Hi all,

I have written a bit of code that cycles through tabled data and matches certain records with other records in the table. I start from the top using a For-Next loop and for every n in the first For-Next loop I run another loop on the same table in order to find the matching entry.

The code works perfectly, but there is one small problem - for some records I need to add an additional record in the dataset, thus increasing the size of the original dataset. I have added a line to increase the LastRow, which is the closing point for both For-Next loops, but the problem is that this does not update in the actual loop, so it still stops at the original LastRow (e.g. original LastRow was 500 and 50 rows were added by the code, resulting LastRow should be - and is - 550 but the loop still closes at 500).

Sorry for the lack of code - thought it would be unecessary given that the issue is pretty simple. I have obviously thought of switching the current For-Next loop for a Do Until loop, using IsEmpty(x) as the closing point, but I am rather partial to the For-Next structure, not to mention that I'm not that keen to rewrite it all now.

Would anyone be able to give me any insight into the above?

Regards,

l.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Maybe post the code


Or make the total row count dynamic (reference the range, but its slower) or add 1 to your total rows when you insert a row?
 
Upvote 0
Maybe post the code


Or make the total row count dynamic (reference the range, but its slower) or add 1 to your total rows when you insert a row?

Sorry, I must have been a bit vague in my post - there is a line of code that adds 1 to the LastRow when a row is inserted. By running the code step-by-step I can see that this IS updating the LastRow, as expected, but unfortunately this update is not reflected in the initial closing point, i.e. the code still stops once it's reached the original LastRow.

I could post the code but have unfortunately left the office...
 
Upvote 0
Maybe post the code


Or make the total row count dynamic (reference the range, but its slower) or add 1 to your total rows when you insert a row?


Charles, how would I go about making the row count dynamic? And do you know whether there is a way for the closing point to update, as you've suggested and I've tried?

Regards,

l.
 
Upvote 0
run upwards through the table which you're adding rows to?
For n=lastrow to 2 step -1
If it's the inner loop, update LastRow within each outer loop.
 
Upvote 0
I was wrong

Neither For
nor For Each

revisits the size of the loop once started

use a while wend or Do Loop

Tried and tested it a few times last week, so I thought as much! In the end I modified it all to a Do Loop, as you've suggested above as well, and simply added a line or two to set the active row as the variable, so I was able to keep the original code. In short - for those that encounter the same issue, I changed the code from this:

Code:
For a = StartRow to LastRow

	xxx

	For b = StartRow to LastRow

		xxx

	Next b

Next a

To this, which eliminates the problem whilst still retaining the familiar For-Next format:

Code:
Cells(StartRow, x).Select

	Do Until IsEmpty(ActiveCell)

		a = ActiveCell.Row
	
		xxx

		Cells(StartRow, x).Select

			Do Until IsEmpty(ActiveCell)

				b = ActiveCell.Row

				xxx

			Loop

	Loop

Thanks again for the help.

l.
 
Upvote 0
run upwards through the table which you're adding rows to?
For n=lastrow to 2 step -1
If it's the inner loop, update LastRow within each outer loop.

Have not had the chance to check this one yet, but will do so tomorrow... looks interesting!
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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