Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

loop and locals window

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

Loop and Locals window

ResolvedVersion 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:

Use the Format Painter to copy formatting more than once in Excel

The format painter tool provides a quick and easy way to copy formatting from one cell to another in Word.

The only problem is that if you click the Format Painter once to turn it on, you can only click and drag over a single cell or adjacent range of cells; then the Format Painter turns itself off automatically.

If you want to copy formatting to cells or groups of cells that are not adjacent to each other, double-click the Format Painter - this way you will be able to copy formatting to multiple cells.

When you have finished using Format Painter, press the Esc key or click on the Format Painter button once to turn it off.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.