Excel VBATutorial                       

             

Excel VBA Macros

Email to excel-vba.com

Excel Tutorial on Macros

 Excel Consulting

Here is a sample of what you will find in lesson 12 of the downloadable Tutorial on Excel macros

Lesson 12 on Excel macros (VBA):

Managing VBA Coding  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.

VBA-syntax-error

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.

Syntax error in VBA

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 learn about them all in the downloadable tutorial on Excel macros. 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:

Error in VBA


We hope you have enjoyed this introduction to lesson 12
for more on this topic and a complete course on Excel macros download the
Tutorial on Excel Macros


Next Lesson: VBA Code for the Application