562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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 ...
Commenting out code in VBA Part one 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.
This blog is part of our Excel macros online tutorial series. Alternatively, come on one of our VBA classroom training courses. |
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!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
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 2023. All Rights Reserved.