In this video we're going to talk a little bit about error handlers so you don't always want your error
that you experience in VBA to open up the actual code window.
That's a way that you could accidentally give your users a backdoor into your direct code and give them
the chance to mess something up in the code or to try to fix something and actually make it worse.
And so instead we build error handlers which let the user typically know hey you did experience an error
and you should probably ask somebody about this but it does not open up the code window like VBA error
would usually do.
So I've got some examples of this here of what your error handlers might look like I'd use the two most
common error handlers which are resume next and go to.
So to break this down for you we'll look up at this top section first.
And this we've created two variables A and B they're both a double type area variable which is going
to be a number and then down here we've set a equal to nine and then B equal to six divided by zero.
Now this should throw an error because we can't divide by zero.
So we should be expecting an error on this line and then just to give us another line after this to
test out the code.
We're going to reset a equal to three.
So you'll notice this is the same across all of these three subs that I have here.
We have creating our variables and then setting our variables with an intentional error creating variables
setting variables with an intentional error and setting variables with intentional error down here as
well.
The difference is this top sub right here has no error handling in it.
So when we rub run this it should go ahead and break into the backdoor of our code like it would normally.
This middle subroutine uses On Error Resume Next which we'll talk about.
And the third subroutine uses on error go to example line which we'll talk about.
So starting up with this code I'm going to go ahead and press F eight to step into this and walk through
it.
So first we set a equal to nine.
Now we're setting B equal to six divided by zeros.
When I press update again we get an error and this says runtime error the error is eleven.
We've tried to divide by zero.
So when we click debug we can see right here this is the line that's giving us an error.
And that's what we expect.
So if I actually end this really quick and I go back to excel and I click on macros and I run that subroutine
which was called intentional error and I click run it will go ahead and break into the back end of the
code for me.
And this is what we don't want to do.
We don't want to provide the users access to our code direct access to the code.
And it's often VBA can be overwhelming for people who haven't seen it before.
So we don't want this window to pop up for people.
So we're gonna go ahead and click end.
Now if I jump back to my code this time we'll look in this particular subroutine which is an example
of On Error Resume Next.
So these four words if you place them at the top of your subroutine they tell VBA if you do experience
an error I want you to just ignore that line just jump to the next line and keep going.
Don't break into the code don't pop up any errors.
Just keep moving forward like you would normally.
This is a little bit risky to use for obvious reasons.
You usually want to know when you have an error you usually do want to be alerted that an arrow was
experienced but sometimes you may have an error that you're aware of that you haven't fixed yet or there
may be unique cases where this is applicable.
So it it is a fairly common tool to address errors.
So if I press F eight to step into this we'll see I've hit this On Error Resume Next line so everything
after this should just skip errors that we experience.
So we're going to set a goal line.
Next we're going to be equal to six divided by zero which we know we can't do and see we've skipped
past this line.
We haven't thrown an error message we're not aware that something's gone wrong.
We'll press a vote one more time one more time and we've ended our subroutine without knowing that we
have an error.
If I go back into my code and I go to macros and I run my resume next public subroutine it's going to
run and it's not going to throw an error.
It's not going to break us into the back of the database at all.
So I'll click Run and we can see nothing happens.
I don't get an error message.
The code runs all the way through.
It just ignored this error that we had.
Now typically like we mentioned you don't necessarily just want to ignore your errors.
It's typically important to know that never was experienced at some point.
So instead we can use on error go to and this is a a way of identifying an error that we experienced
without actually breaking into the back end of the code so we type this line up here on air.
Go to example line and you can change this term to be whatever you want it to be whatever you plan on
calling your section and then essentially rebuild the section further down in our code usually at the
end of your code.
You'll type example line or whatever you named it with a colon.
And this tells VBA This is essentially a label and we can jump to this label using go to this example
line like we had here.
So now we've told VBA on an error when you experience an error I want you to go to this label line which
we created down here and all that we've built here is a little message box that says a we experienced
an error when we were running the code and there are times where you could actually get a little bit
more specific with this.
You could build a couple different of these on air or go tos you could use them a couple different places
in your code where you might experience different different errors and then provide a different message
box to the user.
But in our case we're gonna run through this code.
We put this exit sub line to make sure that if our code runs successfully if we didn't have an error
here if we commented this line out for example when it runs through it's going to exit the sub before
it starts hitting any of these labels and displaying messages that it shouldn't display.
So as a precaution we go ahead and tell it to exit sub at the end of what our actual code is by in our
purposes.
If we run through this we should see it jumped to are labeled line down here.
So going to click into this code I'll press f 8 enter.
Now we've gone ahead and told the system this is what I want you to do when you have an error when you
experience an error here's our airlines.
When I press F eight we see it jumps down to our example line here.
We experienced the error it's going to jump pass everything else and it's going to throw this this message
box up for us and we see an error was experienced during the code will hit OK and now it's just gonna
go ahead and the subroutine without continuing to run.
So again if I go back to my Excel code here and I go to macros and click on the go to location macro
and say run it throws this error message we click OK.
And at no point does it open up the VBA window.
So these are two of the most common ways to address errors in your VBA code.
My preference is to use the go to a certain location and that way we let the user know they did experience
an error but we're not breaking into the back end of our code and showing them this VBA window at all.
So this is definitely something to keep in mind as you're building your code and yet you definitely
need to have some sort of error handling built in so resume next and go to location are both great tools
to have for that.