PDA

View Full Version : Watch Window to jump to cell?



YellowLabPro
07-26-2007, 04:26 PM
When running through a loop can you put a break on a certain cell or tell it to run beginning at a certain cell to a certain cell?
I guess I could do this in my code by changing the start counter and end counter...
But thought maybe the Watch window has this capability? I can only find in the watch window to break on change...

malik641
07-26-2007, 08:57 PM
Hey Doug,

Place the following code in a module:
Sub TestMe()
Dim rng As Excel.Range

For Each rng In Range("A1:A10").Cells
rng.Value = rng.Address(0, 0)
Next rng
End Sub -Click View-->Watch Window
-Then Click Debug-->Add Watch... (or just right-click in a blank area of the watch window and click Add Watch...)
-In the Expression Textbox type in: rng.Address(0, 0) = "A4"
-Yes, that is meant to be rng and not Range (because I'm evaluating the variable rng that I created)
-Select the "Beak when value is True" radio button in the Watch Type frame
-Press OK and run the code. The code will halt when the loop has reached cel "A4".

Hope this helps! :)

Bob Phillips
07-27-2007, 12:58 AM
An alternative is Debug.Assert.

In Excel 2000 and later, you can use Debug.Assert statements to cause the code to break if a condition is <b><u>not>/u></b> met. The syntax for Debug.Assert is:

<b>Debug.Assert (condition)</b>

where condition expression that returns True (-1) or False (0). If the condition evaluates to False or 0, VBA breaks on that line (see, below).

An example usage could be within a loop that has an exit within it to test whether the loop has completed successfully.

Using Malik's example



Sub TestMe()
Dim rng As Excel.Range

For Each rng In Range("A1:A10").Cells
Debug.Assert rng.Address <> "$A$4"
rng.Value = rng.Address(0, 0)
Next rng
End Sub