Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a loop while debugging VBA

The Problem

I am trying to debug some code, and somewhere in the middle I stopped at a breakpoint. Now I want to change some variables and run a certain loop several times.

How far did I get?

I know how to change the variables, but somehow I get stuck when trying to run the loop in the immediate window. Here is an example:

Dim i As Integer 
Dim j As Integer 
For i = 0 To 6
       j=i    ' Do something
Next i 

I tried several variations of the code, but each time I get the following error:

Compile error: Next without for

Other relevant information

I tried searching but mostly found information about problems with loops, whilst I am quite sure the loop itself is fine. (Especially as I reached it before arriving at the breakpoint).

The only place I saw someone addres this situation, he reduced the loop to a single line, however to do this every time would be very impractical in my case.

I realize that I could call a function containing the loop, and then the function call would probably work, but again this feels quite impractical. So I guess it boils down to the following question.

The question

What is a practical way to run a loop whilst debugging VBA code in Excel?

like image 472
Dennis Jaheruddin Avatar asked May 09 '14 15:05

Dennis Jaheruddin


2 Answers

There is actually a way for using loops or other multi-line statements in the Immediate Window - using a colon : to separate statements instead of a new line. Full solution is described here. Note that in the Immediate Window you also don't have to declare the variables using a Dim statement.

To summarize, your snippet would look something like this:

For i = 0 To 6: j=i: debug.Print i+j: Next i 
like image 102
Tamara Aviv Avatar answered Sep 29 '22 10:09

Tamara Aviv


I think I understand your question. You want to run a multi-line code block (i.e. the loop) in the Immediate Window. This throws errors because the Immediate Window is only intended for single lines of code.

I don't have any suggestions other than those you already mentioned. I'd recommend putting your test loop into a separate function and calling that from the Immediate Window:

Sub Test()
Dim i As Integer 
Dim j As Integer 
For i = 0 To 6
       j=i    ' Do something
Next i 
End

Another option is to set several breakpoints. You can also run one line of code at a time with F8.

What is likely the preferred method (i.e., what most people actually do) is use the full power of the IDE, which includes the Immediate, Locals and Watch panes. You can change the value of most variables at runtime by direct assignment in the Immediate Pane (i=6 will do exactly what you think it should do). The IDE also allows you to set breakpoints, add watch conditions, step through code line-by-line using the F8, step through function or procedure calls using Shift+F8, stepping over (and back) through code using the mouse/cursor, and with a few exceptions, you can even add new variables during runtime.

like image 24
PowerUser Avatar answered Sep 29 '22 09:09

PowerUser