Focus video player for keyboard shortcuts
Auto
- 720p
- 540p
- 360p
1.00x
cc
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
How to step through a macro line by line in order to aid in debugging.
Quick reference
Step vs Run
Understanding how to debug code using the Step feature.
When to use
When you need to execute a macro line by line, to see what is happening along the way.
Instructions
Step Into
The Step Into button on the Debugging toolbar allows you to execute the macro line by line.
- The active line of code which is about to be executed will be highlighted in yellow.
- Move the execution point up or down by dragging the yellow arrow next to the highlighted line and dropping it on the line of code you would like to execute.
Run & Stop
- Run the remaining portion of the macro by clicking the Play button (green arrow) on the Debugging toolbar.
- Stop running a macro by clicking Reset button (blue square) on the Debugging toolbar.
Hints & tips
- You can also press F8 to run Step Into.
- Press F5 to run the macro.
- Stepping and running can be used in combination; step the first few lines of code and then run the rest with the Play button (or F5), or stop with the Reset button.
- 00:05 Now the next thing that I wanna look at is,
- 00:08 some of the more critical debugging steps.
- 00:11 This is one of the big pieces that you're gonna end up finding,
- 00:14 when you write code, you will write buggy code.
- 00:17 It's just the way it is, and one of the most important things that we can have,
- 00:21 after being able to read code is the ability to actually debug it.
- 00:25 You'll notice in this particular case here,
- 00:27 I have my workbook Debugging Basics open.
- 00:30 This is, basically reset the schedule back to January 31st, but
- 00:34 the code cleanup changes we made are in here.
- 00:37 I've also docked my Excel window.
- 00:40 Side by side with my visual basic window, I'm going to get rid of
- 00:44 some stuff that I don't need here, so I get a little bit more space.
- 00:47 So I'm just gonna get rid of the project and properties window.
- 00:50 Get rid of the watches window as well, and let's get rid of the locals window too.
- 00:56 So, what we can see is we have our code here.
- 00:59 And what I'd like to do, is I'd like to actually go through and
- 01:03 show you what ends up happening with this.
- 01:05 Normally, when we run our code, it runs from top to bottom.
- 01:10 But what if I need to step through and see what's actually happening?
- 01:13 Well, in this case, there's actually one of the commands, and
- 01:16 I'm just gonna pull this tool bar off so you can see it here.
- 01:18 This is the debugging toolbar.
- 01:21 It will work when it's snapped up into the top here.
- 01:23 Just want to make sure you know which one it is.
- 01:24 You'll notice that there's this little step into- and
- 01:28 if we click on this guy, it highlights the first line of the sub roll forward.
- 01:35 And if we then go and say, all right well let's go and step into it again.
- 01:40 It highlights, it skips over all of the stuff that's commented out.
- 01:42 None of these guys are going to get executed.
- 01:44 But it says, I am going to do this.
- 01:49 And when we step into this one again, it executes that line,
- 01:53 you can see the marching ants are walking around the border now.
- 01:56 And then it says okay, the next line that I'm gonna do, the yellow arrow is
- 02:00 pointing to it, we're going to do a paste special paste values over B6.
- 02:04 So we'll hit step, and you'll notice that indeed it does exactly that,
- 02:10 it copies the values over, and then it says all right, the next line that we're
- 02:13 gonna do is we're gonna do this clear contents, which it does.
- 02:18 We're going to add a formula to A4.
- 02:23 We're gonna copy the formula.
- 02:27 And then I could go, hm, I wanna see something else happen.
- 02:32 Well, actually, you know what?
- 02:33 Let's paste the values again.
- 02:35 Here we go.
- 02:36 So we pasted it as February 28.
- 02:37 Now, what's interesting about playing around in this area,
- 02:40 is that i'm not bound to be working up and down the stack in a normal method.
- 02:44 I can look at this and say, you know what, I want to go see that again.
- 02:47 I am going to go back into this formulaR1C1, so
- 02:50 just drag the yellow arrow back up.
- 02:53 And at this point, what i'm going to do is I'm going to rewrite this guy again.
- 02:58 Which doesn't change the formula,
- 02:59 because it's pointing to the cell above in order to actually do anything.
- 03:03 But now, I'm going to reexecute this line again, copy, and then when I paste it this
- 03:09 time, it actually advances it to March, because I've stepped out of sequence.
- 03:12 So this is what's kind of interesting with the way this works,
- 03:16 is that we can actually step up and backwards and forwards.
- 03:18 And I could say, you know what?
- 03:20 Maybe I don't wanna execute this line for whatever reason.
- 03:22 So I'm just gonna drag down and skip it, and move on to the next line here.
- 03:25 I didn't execute this this time, and now we'll say A6 select.
- 03:30 So you'll notice that I didn't clear the contents of cell A4, it's still here,
- 03:35 and now we could say, end sub, when we actually run that, it steps out and
- 03:39 is no longer in the actual edit or no longer in run mode.
- 03:43 Now something that's really important here.
- 03:45 You guys may recall that I said this before, but I look at this and go,
- 03:49 that's a bit of a disaster, I'd really like to undo it.
- 03:51 But look at my undo stack, totally grayed out.
- 03:54 So in this particular case here, this is where it's really,
- 03:56 really useful to be able to say let's go File > Close.
- 04:00 Nope, Don't Save that one.
- 04:02 There we go. Now notice everything's going with it.
- 04:05 All the workbooks are closing in the Visual Basic Editor.
- 04:08 And now I can go and say, let's go back to Debugging Basics.
- 04:11 We'll bring this guy back up, and here we go.
- 04:12 Our code is good to go.
- 04:15 Notice all of my window frames stay the same as what I had them set before.
- 04:19 So this is really a useful tool, rather than just hitting run,
- 04:24 which will run through everything, we can actually step through individual
- 04:27 components of this particular code to actually let it go.
- 04:30 You can also, for reference, run the entire macro from this area
- 04:33 by just clicking on this little green guy or pressing F5
- 04:36 for that matter, would also allow you to do it.
- 04:39 So if I press run, it will run the entire thing.
- 04:41 You can see it just did its job, or I can step through, or
- 04:45 I can press the F8 key, which will also allow me to go and
- 04:48 execute this line by line to actually see what's happening.
- 04:51 So, this could be a very, very useful tool for going through and
- 04:54 trying to figure out why is your Macro is doing what you told it to,
- 04:58 instead of what you wanted it to do.
- 05:00 Because it always does what you told it to, whether or
- 05:02 not that's what we wanted to do or not.
- 05:05 So, number one secret in programming.
- 05:07 Anyway.
- 05:07 Big key I want you to remember, when a row is highlighted in yellow, it's active.
- 05:12 So if you want to actually get out of it, you either need to run it through the end.
- 05:16 Which we can do, by the way,
- 05:17 by saying Step, Step, Step if I wanna see it run the rest of the way, continue.
- 05:23 Or I could say Step, Step, Step and
- 05:26 I can actually stop the macro from running as well, okay?
- 05:29 So some different ways to actually use that particular feature.
Lesson notes are only available for subscribers.