Locked lesson.
About this lesson
How to use the Watch window to break code execution when variable conditions are met.
Quick reference
Using the Watch window
Using the Watch window for debugging.
When to use
To allow breaking code execution when variables hit certain limits, or just monitoring a variable's value during stepping.
Instructions
Opening the Watch Window
- Go to the View menu on the main toolbar and select Watch window.
Adding a Watch
- Right click in the Watch window and select Add Watch.
- Type the expression for what you want to look for in the Expression field.
- Select your Watch Type option.
Watch Types
Watch Expression
- Allows the code to run and will display the information gathered by the Watch expression in the Watch window.
Break When Value Is True
- Will stop the code running when the value of the Watch expression becomes true.
Break When Value Changes
- Will stop the code running any time the value of the Watch expression changes.
Hints & tips
- You can create multiple Watches for the same subroutine.
- You can edit a watch expression by right clicking on the Watch and selecting Edit Watch.
- 00:05 >> The next window that I want to look at is the watch window which,
- 00:08 you can see down in the bottom right-hand corner here.
- 00:12 And we can get this the same way that we get the others by just going up to
- 00:15 the view tab and grabbing it from the watch window and
- 00:17 that will bring it up into this space.
- 00:19 Now. The interesting part about this this lets
- 00:22 us watch certain expressions to see what happens and
- 00:24 take action on them depending on what we've actually chose to do.
- 00:27 In this case I have a slightly modified version that I had before, that's
- 00:32 simulating that what happens when we get a really long routine and something changed
- 00:36 over time we're trying to keep bugging you know sure exactly what happened.
- 00:39 We sort of expect that one thing is being fed in, but
- 00:41 then we're getting something a little bit different later on.
- 00:43 So what you can see here, if we actually take a look at this particular routine,
- 00:47 is that if I go and actually pick up three cells here, what it's going to do is it's
- 00:51 going to go through and it says i'm going to set the range to selection and
- 00:55 it's going to feed feed me back the address of A3 to A5.
- 00:58 But then you'll notice that this range has been reset and resized.
- 01:01 So instead of actually having a one-by-three block,
- 01:04 we're gonna get a two-by-two block.
- 01:06 It's resizing that range, the new address is A3 to B4.
- 01:10 So it's actually this block right here, which is being copied and pasted.
- 01:16 Next to what we had, now this is interesting.
- 01:20 The key part that I sort of want to look at in this particular case, is I'd like to
- 01:23 go back and say well, what if we're expecting it to only be one column.
- 01:28 So what I am going to do right now is say.
- 01:30 How could I actually figure out, if I go through this how to check that?
- 01:35 So we can set our range of selection and now that we've got a range object I can
- 01:39 actually drill into the object to figure what I can get access to.
- 01:42 So I'll say questionrng.
- 01:44 Dot.
- 01:44 Let's see- if I type in C, I get to cells.
- 01:49 I wonder, do you think it will allow me to actually take a look at columns.
- 01:53 Hey, there we go, columns.
- 01:55 So, we'll go to arrange columns.
- 01:56 Account, and it tells me its 1.
- 02:00 So this is kinda neat, what I'm gonna do now,
- 02:02 is I'm gonna go right click and I'm gonna add a watch.
- 02:05 And that watch is gonna be for rage.columns.count.
- 02:10 And I'll just say watch the expression for right now,
- 02:13 and you'll see that it has a value of 1.
- 02:16 So if I go back and I stop it and try this again, it has no value.
- 02:22 This will say object value variable not set,
- 02:24 because there's nothing been set to it yet.
- 02:26 As we step in, there we are, we assigned the selection.
- 02:30 It's now got one value or a value of one column.
- 02:33 When I go in, the next line that I'm gonna execute, still got a value of one,
- 02:36 but the next line that I'm gonna execute, now says there's a value of 2,
- 02:41 at which point it will do its copy and it will do its paste.
- 02:43 Okay, so this is kind of interesting.
- 02:45 Now, at this point what I'm going to do, is I'm going to go back, and
- 02:50 I'm going to try this again.
- 02:51 And I'm going to say, you know what instead of watching this,
- 02:53 what I'd like to do is I'd like to change this.
- 02:57 And say break when the value changes.
- 03:00 Because now what I can do, is I can come back over here, and I can run the routine.
- 03:04 Watch what happens when I just click Run.
- 03:05 Zoom hit runs until the first time the value changes,
- 03:09 it changed from nothing to 1.
- 03:11 When I run again.
- 03:14 It stops again because it changed the value to two.
- 03:17 Then I can run the whole thing out.
- 03:18 So that looks okay.
- 03:20 It's kinda interesting.
- 03:20 Now what would happen if I ran it now?
- 03:25 We'll have to choose the copyright macro, and it says I'm gonna change,
- 03:28 you'll notice it is changed two.
- 03:31 Why? Because my selection right now has two
- 03:33 columns more run of those.
- 03:36 In every changes again because it has always been two column.
- 03:39 That's kind of interesting facts as well.
- 03:41 So this is how this can actually help us when we are debugging,
- 03:44 because we can see what's actually happening in this particular blocks.
- 03:47 Now the next part that I would like to do is I would like us to use a slightly
- 03:50 different version of this watch as well.
- 03:52 Let's do a different test.
- 03:53 Let's say I'm okay if it shows up with one column.
- 03:57 But I'd like to break only when the column count goes greater than one.
- 04:03 Cuz I've got one column right here.
- 04:05 So, if I run it now, say copyright it
- 04:11 stops where it change it's value to two, two greater than one is actually two.
- 04:17 And I think that's great but what value actually is it?
- 04:20 And a good way to find out is to go back and the add the original watch.
- 04:24 Lets do that.
- 04:25 We'll go right click, we'll say add watch.
- 04:27 And we'll go with RNG.COLUMNS.COUNT.
- 04:30 We'll just use a watch expression here.
- 04:34 We'll say okay, and there we go, rng.columns.count.
- 04:38 So if I had set up a variable to hold this,
- 04:41 it would show up in the locals window nice and easily.
- 04:44 But because I haven't, I can actually essentially use the watch window to
- 04:47 actually build the same kind of a variable thing into play that I can watch.
- 04:52 When it's done, and I hit run all of this stuff goes out of scope the watches
- 04:56 are still there but and I'll stick around, so when I go and actually run this again
- 05:00 and once again it'll stop in the figure case and say the columns are greater than
- 05:03 one because you actually have two calls in your selection now, that's all good but
- 05:08 I can actually see all those individual components which is kind of cool.
- 05:12 So it's another useful tool to actually help you get to the bottom of
- 05:15 why things are changing on you when you may not expect it
Lesson notes are only available for subscribers.