debugging - stepping through a loop

S

scottydel

Hello,

I'm using Office 2003 and am stepping through some code by setting a break
point and using the debugger. My question is this, is there a way for a
break point to stop execution at a specific iteration of a loop?

For example, i have a for loop that iterates 16 times. I am only interested
in the last 4 iterations. Is there a way to set the break point to start
debugging after "x" iterations are complete, rather than stopping execution
at the first iteration, and having to step through the several iterations
that I am not interested in?

Any help would be appreiated.

Thanks,

Scott
 
T

Tom Y

Scott,
Use an "If statement with the loop. Something like this.

Dim intCounter as integer
For intX = 1 to 16
intCounter = intCounter + 1
if intCounter >= 13 then Break
...Do your stuff
Next intX
Hope this helps.
 
S

scottydel

Makes sense, but "Break" is coming up as undefined.

Again I'm using Office 2003 (Excel in this case).

Could it be that "Break" is only available in a full-blown version of VB,
and not VBA?
 
J

Jonathan West

scottydel said:
Makes sense, but "Break" is coming up as undefined.

He meant Stop, not Break. But better still is to set a watch on the
variable, and break when it reaches a specific value.
 
S

scottydel

I added a watch for the loop counter, then clicked "Edit Watch", and the only
choices I had were:

-Watch expression
-Break when value is True
-Break when value changes

There is no option to Break for a specific value. Am I missing something?

"Stop" however worked, thanks.

-Scott
 
J

Jonathan West

scottydel said:
I added a watch for the loop counter, then clicked "Edit Watch", and the
only
choices I had were:

-Watch expression
-Break when value is True
-Break when value changes

There is no option to Break for a specific value. Am I missing something?

Yes. Make the expression that you watch something like this

intCounter >= 13

Then set to break when value is true.
 
S

scottydel

Again, that makese sense but didn't work. I made intCounter >= 13 the watch
expression, set it to break with value is True,
but it does not break. It says this under Value of the watch:

<Expression not defined in this context>

Just to make sure, I created a boolean variable, blnTest, set it to True
when intCount >=13

Dim blnTest As Boolean
blnTest = False
For intCounter = 1 To 16
If intCounter >= 13 Then blnTest = True

<rest of my loop>

Next

Then I added blnText to the watch, and set it to break when the value is
True, and it worked.

So it seems a boolean variable as a watch can break when the value is true,
but an integer comparison that should result in a boolean value, does not
break when the value is true.

Again, I am only using VBA and do not have Visual Studio available to see if
this is just a limitation of the Office VBA Editor.

You still think I'm missing something? It seems like it should work...

Thanks,

-Scott
 
K

Klaus Linke

Hi Scott,

Strange, works for me... Word 2003, VBA editor.
Maybe try again?

Dim intCounter As Long
For intCounter = 1 To 16
Debug.Print intCounter
Next

(set watch for intCounter>=13, stops when intCounter reaches 13)

Regards,
Klaus
 
P

Patrick Pirtle

It will give that message when the code is not being
executed. Also, in the "Edit Watch" dialog, be sure
the "Procedure" and "Module" are set correctly.

HTH
____________________________________________
The impossible just takes a little longer...
 
K

Karl E. Peterson

scottydel said:
Again, that makese sense but didn't work. I made intCounter >= 13 the watch
expression, set it to break with value is True,
but it does not break. It says this under Value of the watch:

<Expression not defined in this context>

What Patrick said. The problem here is, as the error message says, context. You
probably didn't specify which module and routine to watch within. I find it easiest
to add these critters by putting a break point within the procedure I want to watch,
execute to that point, set the Watch, and press F5 to continue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top