Return to VBA Code Examples

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

 

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!