BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Commenting code is an art form! This series of blogs explains why, how and when to comment in VBA.
- Why Write Comments in Excel Visual Basic
- Adding Comments in VBA macros for Excel (this blog)
- Commenting Out Code in Excel Visual Basic
Posted by Andy Brown on 28 April 2011
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.
An example of commented code in Visual Basic for Excel
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:
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:
It seems easier to this owl just to use repeated apostrophes!
Note that comments don't have to start at the beginning of lines:
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:
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:
To change the comment colour:
You can also comment out code, as described in the next blog in this series.