Locked lesson.
About this lesson
How to set and use breakpoints during code development and debugging.
Quick reference
Using Breakpoints
Introduction to Breakpoints.
When to use
To allow stopping execution at key points when running code. (This is faster for debugging than stepping through each line of code you know is working correctly.)
Instructions
Inserting a Breakpoint
- Click in the left-hand margin next to the line of code where you want the Breakpoint to be inserted.
- The line of code will be highlighted in red and a red dot will appear to the left.
Running Code with a Breakpoint
- When running a macro with a Breakpoint the code will stop on the line where the Breakpoint is located (without executing it)
- When you hit a Breakpoint, you may then continue by either stepping or clicking Run
Removing Breakpoints
- Click on the red dot to manually to remove the Breakpoint.
Notes:
- You can insert multiple Breakpoints into your code.
- While the code is at a Breakpoint, you can work back and forth between Excel and VBA
- Breakpoints cannot be saved in your code. Reopening the project will cause all Breakpoints to be reset.
- 00:05 There's another tool that works very, very well with stepping and
- 00:09 that's called a break point.
- 00:10 Now you'll notice in this particularly case that I have reset the file back
- 00:14 to the same debugging based file that I used for our stepping video.
- 00:18 So it doesn't have any of the additional stuff that hasn't been rolled forward
- 00:21 at all.
- 00:22 And I want you to start thinking about what happens when this code gets longer.
- 00:27 Right now it's only eight lines long.
- 00:30 It's not really a huge piece of code.
- 00:32 But what happens when it rolls out to 80 lines or 800 lines?
- 00:36 And I hope you never write a macro that's 800 lines.
- 00:39 I'll tell you why later.
- 00:40 But when it starts getting longer you may find that there's certain components that
- 00:45 you debugged your way through and you're happy they're working but
- 00:47 then you get down near the end, and you need to stop, and
- 00:50 actually look at your code in a little bit more detail to figure out what's going.
- 00:54 Now stepping, if that code block is 80 lines
- 00:59 down stepping 80 times to get in there is kind of painful and
- 01:03 a little bit tedious, and is gonna give you a cramp in your finger.
- 01:06 So we don't really want that.
- 01:07 What we would love to find is a way where we can actually run up to a certain
- 01:11 point and then stop there.
- 01:14 So to do this, what we gonna do is,
- 01:16 is we are gonna use the break point to make that happen.
- 01:20 Now if I look at this code here, let's say that, you know what,
- 01:23 these first three lines I'm happy with the way they were worked on.
- 01:26 I know that they've copied.
- 01:27 We've pasted.
- 01:27 We've cleared the contents here.
- 01:29 But I'm really interested in seeing what is going on inside this particular area
- 01:33 with the formula, maybe I want to try and
- 01:35 get this roll forward into a proper quadrant.
- 01:37 So what I am gonna do is I am going set up a break point on this particular line
- 01:42 because I want to be able to run my code until it hits this point and then stop.
- 01:47 To do this, what I am gonna do,
- 01:48 I don't even need to highlight anything over the margin here beside my code,
- 01:52 I can just click and it will create what's called a break point.
- 01:56 This red line with a dot.
- 01:58 What happens at this point is that I can still step into my code.
- 02:02 You'll notice that I can step.
- 02:04 I can step.
- 02:05 I can step.
- 02:05 It's doing the copy.
- 02:06 Now I'm not gonna paste at this point because I just wanted to demonstrate that
- 02:09 these work with stepping.
- 02:11 So I'm just gonna reset this now, and send it back out.
- 02:14 The other place that they work though is they work by running.
- 02:17 And I could run it by pressing the green arrow here, that'll run it.
- 02:21 I could even kick off my break point by clicking on the RollForward button, and
- 02:24 you'll notice that it actually executes all of these three steps.
- 02:27 So it's copied.
- 02:28 It's pasted.
- 02:29 It's cleared the contents in the content area, and
- 02:33 it's now stopped on the red line, ready for
- 02:36 me to actually look at this piece, so I can say, okay this is interesting.
- 02:39 So now I can step and that will create my formula for me.
- 02:43 I go that's not really the formula I was looking for
- 02:47 I wanted to advance it three months, so I could actually look at this.
- 02:50 I can say, well okay, I need on the EOMONTH function,
- 02:54 I wanted to advance it three months which will gave me April 30th.
- 02:59 Okay, so if I'm looking at that function, I take this back.
- 03:02 I see this is EOMONTH R1C, okay.
- 03:05 But it's got the comma here, so this is what I really wanted was this three.
- 03:10 Now what I'm gonna do is I'm gonna grab my yellow arrow, bring it back over here, and
- 03:15 I'm gonna delete this formula from the Excel worksheet.
- 03:17 So this is really cool because you can actually work back and
- 03:19 forth between Excel and VBA while the code is running live, that's awesome.
- 03:24 Now with this, what we can do is I move this cursor back,
- 03:27 I've deleted the formula, so I'm gonna try it again, we'll see if we get it right.
- 03:31 Step into, bingo, awesome that looks beautiful.
- 03:34 So now I'm really happy with the way the code is working, now I could copy and
- 03:38 I could then paste values, there we go.
- 03:41 Clear contents and select, and we're done.
- 03:45 Okay, perfect.
- 03:46 So that looks like it's working quite nicely.
- 03:49 How many break points can you use?
- 03:50 Well actually you can use lots.
- 03:53 So if I now hit run, you'll notice it stops.
- 03:57 I could run into the next one and I could say, hey, I really want to step
- 04:00 through this particular component and then I can run the rest of the way out.
- 04:05 Okay, so I can use lots of these guys all at once.
- 04:08 Now, do these stick around?
- 04:11 Can I release code with a break point in it?
- 04:14 Lets go file, save as, and I'm gonna go, and
- 04:18 put it in a different folder than the step vs run here.
- 04:20 Were gonna go back to using break points and I'm just gonna call this one.
- 04:25 Breakpoint, oops I'm trying to spell that correctly.
- 04:28 Here we are.
- 04:30 And now I'm gonna say File, Close.
- 04:31 File, Open and noticed that none of the break points are in my code anymore, so
- 04:37 I can't release with break points and this is good because we don't really want
- 04:43 to run launch the user into the Visual Basic, and I just make some freak out.
- 04:49 So this is a great technique cuz let say,
- 04:51 they works very well the compliment to stepping when your debugging.
- 04:53 You can run it through to the big block code you wanna check without having to
- 04:57 step three individual piece and then continue on from there.
Lesson notes are only available for subscribers.