Microsoft Excel Macros

 

VBA Lesson 12: VBA for Excel to Manage  Errors

The Visual Basic Editor will help you avoid errors in coding in many different ways. You will not have to wait at the end to be told that there is something wrong with your macro.


Spelling Errors

You have seen in lesson 11 the VBE capitalise letters to let you know that there are no spelling errors.


Syntax Errors

The VBE will also tell you that there is a syntax error in what you have just written by making the font red and showing you a message box.

Exercise 1

Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor (VBE).

Step 2: In the code window of any of the sheet copy/paste the following line of code: Range(A1").Select and click "Enter".

You get the following message box telling you that you are missing a "list separator". Look for the error before the segment highlighted in blue. We can deduce that VBA is talking about the missing quotation mark.

Step 3: Click on the "OK" button.

Step 4: Add the missing quotation mark, use the mouse to move the cursor to the end of the sentence and click "Enter". The font is black meaning that everything is correct.

Exercise 2

Step 1: In the code window that you have used for exercise 1 copy/paste the following line of code:
Range("A1".Select
and click "Enter".

You get the following message box telling you that you are missing a "list separator". Look for the error before the segment highlighted in blue. We can deduce that VBE is talking about the missing parenthesis. Both the quotation marks in the exercise above and the parenthesis in this exercise are considered as "list separator" by the VBE.

Step 2: Click on the "OK" button.

Step 3: Add the missing parenthesis, use the mouse to move the cursor to the end of the sentence and click "Enter". The font is black meaning that everything is correct.

Step 4: Close Excel without saving anything


There are many other ways that the VBE uses to alert you to coding errors. You will also learn how to use "If" statement to catch errors during the execution and how to use the OnError statement to generate user friendly error messages like the following:


Go to the next lesson
Lesson
13 : VBA for Excel for the Application


left arrow Back home