In this video we're going to talk a little bit about the debugging methods and debugging tools you might
use to help identify errors in your code and address them.
And so I have here an Excel file that we built in our form controls and form code videos early on when
we discussed how to build user form and use a user form in excel.
And so what we have in this particular form is I can add employees to this table and I can select whatever
role they're in.
Enter a first name.
So we'll just do an example we'll just say Hannah Smith and we won't check full time so we'll just click
add and we can see the employee has been added to the table and on my actual table we can see that her
name does appear here with the information that we selected.
So we're gonna go ahead and go to the developer tab and we're going to open up our visual basic window
and I'll go ahead and I'll make this full screen.
Now I'm going to go ahead and I'm going to open up the code that appears in my actual form by right
clicking and selecting view code and we can see here this is all the code that's tied to my form itself.
And so we're going to talk a little bit about different methods we might use for debugging and identifying
errors in our code.
So up here you can see VBA actually has a debug menu that you can click on and we can see step into
step over step out run to cursor and watch quick watches that it watches breakpoints et cetera.
And so this is what we're gonna talk through in this video are these particular tools and how we can
apply some of these tools to our code.
So we've actually used some breakpoints before and this is actually what appears when you click to the
left of a line and it highlights this line in red.
And this means this line is a break point for our code.
So as our code runs through this particular subroutine it's actually going to break on this line and
give us access to our code so we can make some adjustments or see where we are at in the code.
So I'm going to break a little bit later.
I'm going to break on this roll line and I'm going to go back to my excel file.
Go ahead and add an employee and I'll just fill some random values in here that we can just get an idea
of what's going to show up and I'm going to click Add we can see it has successfully broke the code
on this specific line that we asked it to break on and this is a good way to pause your code.
Maybe after you've assigned all your variables or something so you can come back through and you can
actually check what your variables are.
So I can hover over my first variable my last variable my start variable and I can see what values are
currently stored into all of those.
I can also see right now my role doesn't have an assigned variable and that's because I haven't actually
executed this line of code yet.
So wherever you add a break it will pause on that line before running that line.
Now if we want to step through this line by line we can either use debug step into or we can just press
f 8 on our keyboard.
So I will hit f 8 and we can see now we've moved past this roll line.
If I hover over roll I can see we've assigned it the value of sales run to cursor is another helpful
tool.
So if I move my cursor down say this is the next line I want to stop on rather than adding a break if
we don't want to have to add another break into our code we can use debug run to cursor or control and
evade and we can see this time it ran directly to where I had my cursor which was on this line.
It has actually executed these in-between lines I can see that when I hover over those variables and
I can see what those values are.
So this debug menu up here has some really helpful things in step into run to cursor.
We've talked a little bit about our breakpoints and things like that.
So the next thing we'll talk about is adding a watch and what a watch does it is in this window down
here.
And this will show up when you add a watch or edit a watch.
You can assign a watch on a particular variable or something that you're interested in tracking throughout
your code and then as you run your code you can see how that variable changes values or contacts or
type or anything like that.
And so it's super helpful for if you have a really long piece of code where you're using the same variable
for multiple values or maybe you're doing some math on a variable or something and you want to see how
that variable changes so we can select a variable we'll just do this for our first variable here as
an example and we will add a watch and we can see it's automatically selected this first expression
because that's what I had my mouse on when I when I actually clicked on this and it shows us the context
that we're running this in and the module that we're running this in.
And so all this means is we're in this particular Add button subroutine here and we're in our ad employee
module which is just the code that's tied to our ad employee form.
So I can click OK and we can see this has showed up down at the bottom of our screen here.
And the variable is first this is what we're tracking we can see right now we've stored the value F
into this variable which we could also figure out by hovering over the variable.
But if you wanted to do this with a bunch of watches instead of having to outbreak and then manually
come through and hover over each variable to see its value you could do that with watches instead so
watches are another very helpful tool for debugging and seeing if your variables are reading correctly.
You can also build message boxes in at strategic points in your code for testing purposes.
So if I don't want to have to use any breaks and I don't want to have to use any watches or anything
like that I just want to run my code cleanly and I want to know when I get to certain points in my code
I could build a message box.
So for example I could build a message box here that says the variable R is and then show me the variable
R and when I run this line a message box will pop up on my screen that shows me what my variable R is.
So I know at this point OK I've successfully hit this line in my code.
I know that my R variable is whatever it is being displayed on my screen so it'll give me an idea of
OK.
I can successfully run to this point in my code and I know my variable R either is or isn't pulling
correctly.
And so you can use message boxes to build in little checks in your code just for developer purposes
and then later on you can come back through and you can comment out these lines with an apostrophe and
that way it makes a little easier for you if you ever run into an error in the future you can come through
and turn those message box lines back on and get an idea of where your error is actually existing.
Now one of the most common types of errors that you'll get in your code are variable errors and this
generally references when you have a variable that you've declared incorrectly.
And so what that means is if we declare this variable the first name variable for example as an integer
instead of as a string then later on in our code we're gonna get a little bit of an issue when we try
to assign a text value of F to a variable that we have predetermined should be an integer and that'll
throw an error.
You can also get an error if you assign text to a variable that you later on try to perform math on
that would also give you an error because you can't perform math on just text.
So the most common error is variable mismatch error which is what we just discussed.
So if you ever have some variables that don't seem to be pulling correctly or that are throwing errors
that's the first thing that I would go in and check is make sure that you're type that you've declared
your variable as matches up to what that variable is actually representative of another common error
is performing work on the wrong sheet or reading data from the wrong sheet or you know wrong workbook
in excel.
And this goes back to when we were talking about the object hierarchy and how important the object hierarchy
is we can see here I've gone ahead and specified I'm going to activate my sheet one worksheet and all
of the code that follows will be applied to this active worksheet which is Sheet 1.
It's important that we keep that in mind as we move through our code and we make sure we're always looking
at the right workbook in the right worksheet in the right range.
And so this is something that also causes a lot of error drives a lot of error later on is if we're
not paying attention to what is our active worksheet or our active workbook and are we referencing the
right worksheet in the right workbook in the right data.
And so this is a second thing that I would check if you ever are getting an error or your code isn't
running as you expect it to now VBA is also very good about actually giving you an error code.
Anytime it encounters an error.
And so generally speaking if we have an error that our code experiences while it's running VBA is going
to go ahead pause it's going to pull up this window it's going to highlight the line just like this
is highlighted.
It's going to highlight the line that is giving an error and it's going to give you a little pop up
window that has an error code in it and that error code is something that you can actually Google or
you can get on Microsoft's help site which is a great resource and you can figure out exactly what that
error code means.
And that gives you a more specific idea of what you need to address.
And you know the issue that is existing is on whatever line is highlighted so you know what you need
to address and where you need to address it.
So VBA actually has its own built in error addressing functionality that makes it a lot easier for you
to address any errors that you are getting.
But with that being said you don't necessarily want your code to break and open up for any user who
happens to be in the code so you don't necessarily want a file that you're pushing out to a big group
of people to have an error that lets your users get into the background code and mess up the background
code.
And so there are some error handlers that we can build into VBA that will take an error and process
it internally in the code without opening up the code or any sort of window for the user to interact
with and potentially mess up your code.
And so that's what we'll be covering in a later video but this video to be discussed the various methods
you can use to debug your code.
Some of the tools that VBA has built into the debug menu which are very helpful.
And we talked a little bit about some of the more common errors that you'll experience in VBA and what
to watch out for.
As your coding.