Syntax Error VBA

This tutorial will explain what a VBA Syntax Error means and how it occurs.

VBA code has to be constructed in a certain way in order to run. A syntax error occurs in VBA when the code is not constructed correctly.  These errors are shown in the VBE Editor in red which makes them easy to identify.

(See our Error Handling Guide for more information about VBA Errors)

Warning Messages for Syntax Errors

If you have the option for Syntax Check switch on in your VBE, then a message box will appear when you make a syntax error.

VBASyntaxError MsgBox

If a message box does not appear, the incorrect code will still show up in red as in the graphic above but a warning message will not be displayed.   It is up to you whether or not you want these warning message to appear as you can switch this option

To switch on this option, in the Menu, select Tools > Options.   In the Editor Tab, make sure that the option Auto Syntax Check is ticked.

VBASyntaxError Options

 

Common Syntax Errors

Syntax errors occur due to misspelling or missing code – like the omission of a Then when we are writing the first line of an If statement.

Some other common error are listed below.

For without a To :

VBASyntaxError ForTo

Do Until without specifying the condition:

VBASyntaxError DoUntil

With without specifying what is included in the with:

VBASyntaxError With

 

Not Closing Brackets or Leaving out Punctuation:

VBASyntaxError Brackets