Excel VBA Introduction Part 3 - What to do When Things Go Wrong (Errors and Debugging)

Excel VBA Introduction Part 3 - What to do When Things Go Wrong (Errors and Debugging) Welcome to this was our tutorial in this video we're going to cover errors and debugging in Excel VBA or what to do when things go wrong and trust me there's plenty of things that can go.

Excel VBA Introduction Part 3 - What to do When Things Go Wrong (Errors and Debugging)

Wrong when you're programming in VBA we'll start by looking at the various types of errors you're likely to encounter in VBA and those are syntax errors compile errors and runtime errors.

We'll cover each of the different types tell you when they're likely to occur and what you can do to solve the problem when they do occur after we've done that we'll show you a few debugging.

Techniques as well so talked about how you can step through your code line by line how you can set breakpoints which allows you to run your code up to a certain stage and then how you can.

Display and use the debug toolbar as well so let's get started so in this video we're going to show you some of the things that can go wrong when you're writing and running VBA code and I speak.

From bit of experience when I say there are many things that can go wrong when you're when you're working with VBA but let me show you some of the most common things I think you'll encounter the.

First type of problem you're likely to encounter in VBA is called a syntax error this is essentially an error in the grammar or the structure of an instruction that you've written so.

Usually misplaced punctuation like double quotes or full stops etc it's very easy to spot syntax errors because pretty much as soon as you make the mistake you revolve that you can pretend.

Something wrong so for instance if I remove this closed parenthesis on one line of code as soon as I try to move away from that line of code it's highlighted for me in a couple of.

Different ways that I've made a mistake first of all if you can see it the text is turned red in the background and I've also got a dialog box which appears that tells me I've made what's referred to as.

A compile error and it actually gives me a sensible suggestion in this case it says it expects a list separator or a closed bracket I don't know if you can also see is highlighted the piece of.

Code where it thinks that lists a separator or close bracket should appear if I click OK simplex R is a really easy to fix you simply have to modify the grammar so that your sentence make sense.

Posts Related:

    So if I pop my closed round bracket back

    In again click on a different line to confirm it and as my syntax are affixed now it's actually very easy to confuse the syntax.

    Error message dialog box by making a different type of mistake in this example what I'm going to do is remove the set of double quotes after the cell reference and if I click on another line.

    Of code to confirm that I've entered that line I get the same dialog box appearing telling me it's miss is expecting the list separator or close bracket it's highlighted the word.

    Created in my in my line of code it's clearly a syntax error because I've got the line of text highlighted in red but it's clearly picked up completely the wrong thing this time so although some.

    Of these some of these error messages can be useful the syntax error messages that can be generated very very easily confused I'm going to click OK.

    And I'm going to fix the problem that I've deliberately created click on under the lines confirm it my personal preference is to not display the dialog box if I make a syntax error you can.

    Actually switch those off by heading to the Tools menu in the VBA editor choosing options and then unchecking the box that says auto syntax check once I've done that if I click OK I can still.

    Make syntax errors and I can still get my syntax errors highlighted for me if I'd make the same mistake again I can take away the double quotes click on another line of code and as long as you.

    Can see it it's highlighted in red so I've clearly got a problem with that line from a previous video we mentioned that if you can't see red and green colors you can always head to the Tools.

    Menu choose options choose editor format

    Select the type of text you want to modify such as syntax error you can actually choose a different color to make you stand out for you so personal.

    Preference as I say is not to have the the syntax error message dialog box pop-up I find it gets in the way I think it's more than sufficient tap a line highlighted in red to indicate that I've.

    Made a mistake there needs to be fixed okay so the next level of error we can look at is something called a compile error compiling is something that happens for you automatically whenever.

    You try to run a subroutine it's basically Excel VBA a way of sense checking the lines of code that you've written so here's an example of a compile error if I accidentally or.

    Deliberately misspell the word range in one of my lines of code notice that when I click away from that line is not highlighted as a syntax error so syntactical occur automatically that.

    Line does make sense I've got all the right words and all the right punctuation characters in the correct position it's just a misspelling of the word that is the problem so if I try to.

    Run this subroutine what's going to happen is I'm going to get a compile error message and compile errors as some of the most useful ones because they highlight almost exactly where the.

    Problem is it tells me that that key word has not been recognized or civil function not defined in VBA speak if I click OK I can fix the problem by simply retyping the word as range you might.

    Also notice that this first line of your subroutine is highlighted in yellow indicating that is trying to run at the moment what you're in at this point is something called break mode so you can.

    See at the top of the screen is a little word break in square brackets usually the best policy is to reset your subroutine before you attempt to do it again so you hit the reset button this.

    Little blue square we could also head to the run menu and choose reset from there as well and then you can attempt to run your program again now compile errors don't actually crop up at the point you.

    Run a subroutine compile errors crop up immediately before your subroutine runs so it's possible to highlight compile errors without choosing to run a subroutine you can do it by if I get.

    Again make the same mistake by misspelling range I can head to the debug menu and choose to compile my entire project so this will go through in sense check every line that I've.

    Written without attempting to run anything so I do that again I'll get exactly the same message that I saw earlier compile error Sybil function not defined I have.

    - click okay fix the problem before I can successfully run this program notice this time that your subroutine isn't in break mode so I don't have to hit the reset button before I can try to do.

    Anything else while we're talking about compile errors it's worthwhile mentioning another technique that you can use to make the compiler even more fussy than it currently is to actually.

    Give you more useful error messages as we try to learn and and compile your projects so for instance without this technique turned on I can make mistakes such as misspell the names of functions.

    Such as a date that's a cover take whether ye I can also make spelling mistakes in in references to constants such as color constants so again if I misspelled this in some way I can't.

    Spell turquoise or the best of time so I take away the the I there now what would happen normally if I try to run or compile my project if I if I choose to compile it first of all I don't get any.

    Compile errors if I try to run my subroutine I'll find that it actually works it's performed a job so it should have MIT put the date in cell b2 and it should have changed the background color.

    Resulted to pale turquoise but if I go back to my to excel itself I'll find that first of all the date doesn't appear in cell b2 instead of pale turquoise background color of those.

    Cells is black now I can make my compiler pick up on these mistakes that I've made by adding a couple of extra keywords to the top of my module if I go back to the visual basic editor and.

    Before the subroutine that I've written at the very very top of the entire module I'm going to write these two words option explicit now these two key words aren't actually designed for this.

    Purpose option explicit is designed for working with variables which is a subject for a later video but we're going to use it at this point to help us pick up on the spelling mistakes that.

    We've made so with option explicit turned on if I head to the debug menu and choose compile project it's going to pick up on the very first misspelled keyword that it finds so it doesn't.

    Recognize the word that and I haven't declared a variable called that and as I say we'll talk about variables later on that's just to explain that the message that you actually see.

    So if I click OK I know that there's a problem with that specific keyword there if I spell that one correctly again I can now try to compile the project again and this time you should pick up.

    On another keyword that I've misspelled so again it says variable not defined it recognize a keyword and I haven't declared a variable called RGB pale Turk was so I click OK if I can I'll spell.

    Turquoise correctly there we go and this time when I debug and compile my project everything passes the compilation personally I always choose to have option explicit at the top of a module.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continuetomake videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=DlqKfYTgc9Q
Postagem Anterior Próxima Postagem