Commenting out code in VBA
Part two of a three-part series of blogs

Commenting code is an art form! This series of blogs explains why, how and when to comment in VBA.

  1. Why Write Comments in Excel Visual Basic
  2. Adding Comments in VBA macros for Excel (this blog)
  3. Commenting Out Code in Excel Visual Basic

This blog is part of our Excel macros online tutorial series.  Alternatively, come on one of our VBA classroom training courses.

Posted by Andy Brown on 28 April 2011 | no comments

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Adding Comments in VBA

This online training blog shows how to add comments to VBA (Visual Basic for Applications) code.  The ideas shown work in Access, Word, PowerPoint and all other MS Office applications, but the diagrams come from Excel Visual Basic.

Example of commented procedure

An example of commented code in Visual Basic for Excel

Writing Comments

Any comment in VBA begins with a single apostrophe character (').  When you type in a comment and press the Enter key, the line will go green.  For comments spanning multiple lines, you'll need to type the ' key multiple times:

Example of multiline comment

This comment runs for 2 lines

Note that there is one other way to get multiline comments, and that's to use an _ underscore continuation character after all but the first lines:

Using underscore for multiline comments

It seems easier to this owl just to use repeated apostrophes!

Note that comments don't have to start at the beginning of lines:

Example of Comments Midline

Anything after the apostrophe in each line will be ignored by VBA

Changing the Colour of Comments

If you don't like the colour green - change it!  The first thing to do is to go into the VBA editor's options:

Tools / Options menu

First, go into the VBA code editor's options dialog box as shown on the left.


Now choose to change the colour for your comments as follows:

Changing comment colour dialog box

To change the comment colour:


You can also comment out code, as described in the next blog in this series.