loop and locals window
RH

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Loop and Locals window

Loop and Locals window

resolvedResolved · Low Priority · Version 365

Qasim has attended:
Excel VBA Intermediate course

Loop and Locals window

Initially in MyModule, we set x = 1 to 5 and y = 1 to 10. When we press F8 to step through the code, it shows:
x = 1, y = 1
x = 1, y = 2....
eventually we get:
x = 1, y = 11 even though y ends at 10.
Any idea why this is?
It also occurs at the end of the run and shows x = 6, y = 11.

RE: Loop and Locals window

Hi Qasim,

Thank you for the forum question.

Yes if you use a For Next loop For x=1 to 10, it will count to 11.

The logic is that you type the line For x=1 to 10 above the code you want to loop trough. When you step the code (F8) you can see in the Locals Window, that you add 1 to x every time you reach the line Next x. When you have 10 in x you still want to run the lines
under the line For x=1 to 10 one more time (it will be the tenth time). When you get to the line Next x you will get x=11 and the loop will stop.

I hope this makes sense.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector


 

Excel tip:

Hiding and unhiding rows using the keyboard

CTRL + 9 hides your columns and CTRL + SHIFT + ( unhides them although you would need to highlight the row letters either side as per normal

View all Excel hints and tips


Server loaded in 0.06 secs.