Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
All good programmers use comments liberally. To see why, consider the following excerpt of code, taken from Excel VBA:
Range("A1").Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = InputBox("Choose a Wise Owl")
Now compare this with exactly the same code, but commented:
'go to the bottom of column A
Range("A1").Select
ActiveCell.End(xlDown).Select
'go one cell further, to first blank cell …
ActiveCell.Offset(1, 0).Select
'… and set the value of this cell to be the Wise Owl name typed in
ActiveCell.Value = InputBox("Choose a Wise Owl")
We think the second code excerpt is far easier to read and understand!
Here are 2 good reasons to use comments in your code:
Reason | Notes |
---|---|
Intelligibility | You may understand your code now, but will you know what it was meant to do when you revisit it in 6 months' time? Will your colleague understand it when you leave your company? Comments make code easier to read and follow. |
Clearer thought process | It sounds strange to say it, but commenting code as you write will help you think about what you're trying to do. If you write the line: 'go to the bottom cell in the first column you've already thought about what you're trying to achieve, and are more likely to translate this into code correctly. |
As a rough guide, you should add a comment for every 2-3 lines of code on average.
That's why you should comment your VBA code - now read on for how to do it!
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
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.
Rather than have to type an apostrophe before every line of code, you can select a block of code and comment it out:
Select the code to comment out ... | ... to tell VBA to ignore it |
To do this, you first need to display the correct toolbar.
The easiest way by far to comment out code is using the Edit toolbar:
Right-click on any toolbar in VBA and tick the Edit toolbar to display it
Once you have this visible, you can comment code out as shown below.
To comment out code, first select part or all of the lines of code you want to comment out:
You don't have to select all of each line (here we'll comment out the 3 lines of code wholly or partially selected).
You can then comment the lines out:
Click on the tool shown to comment out selected lines (or the one to its right to comment them back in)
Commenting out code is particularly useful for lines copied from the Internet.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.