This post may contain affiliate links. Please read my disclosure for more info:

1.5 Adding Comments

Beginning VBA

Beginning VBA

Previous Part: 1.4 Understanding the code

In this part, we are going to consider one of the most important, but least exciting elements of writing VBA code – comments.

 

Comments

Comments are pieces of text included in the VBA code to describe what the code is for.  This comment text does not interfere in any way with the VBA code because when the code is executed all comments are ignored.  So, the comments serve no purpose what-so-ever when running the code, which means you could leave them out entirely.  However, to do so would be a big mistake.

The comments are there for your benefit, or the benefits of anybody reading your code.  When you return to the code, even if just after a few days, you won’t always be able to remember what the piece of code did.  With small Macros which are just a few lines it may be easy, but once the lines of code expand these comments will save your hours of time.  Imagine writing 100 lines of complex code, then returning to it 6 months later to make a small change.  Without the comments, you will have to understand the logic of the code all over again – a big waste of time.

At the time of writing the code it may seem like too much effort to document what is happening, but at a future point, you really will wish you had taken the time.  So, take the time – you’ll be glad you did.

 

How to make comments

Comments are always preceded by an apostrophe ” ‘ “.

A comment can be a whole line of code:

Sub HelloNewWorld()
'This is a whole comment line - this code opens a message box
MsgBox "Hello new world!"
End Sub

A comment can also be a partial line of code:

Sub HelloNewWorld()
MsgBox "Hello new world!" 'This is a partial comment line - this code opens a message box
End Sub

Unlike other programming languages, there is not a method to close a comment.  Once you have inserted a comment, everything on that line after the apostrophe will be part the comment.  The comment closes as soon as you start a new line.  If we wish to have a multiple line comment, then we should use an apostrophe on every comment line:

Sub HelloNewWorld()
'Comment line #1
'Comment line #2
'Comment line #3
MsgBox "Hello new world!"
End Sub

 

Tips for good commenting

The following are just a few tips to write good comments:

  • Always insert comments at you write the code
  • Insert comments at the top of every procedure to describe it’s purpose
  • When writing a complex procedure use comments to clearly divide the code into understandable sections
  • Insert comments to describe the purpose of variables
  • Insert comments to describe the reason for taking one coding choice over another
  • Insert comments where the purpose of any line is not blatantly obvious.
  • Insert comments when you use a workaround or non-standard method.

 

Using comments whilst debugging code

There will be occasions when a piece of code does not function how you expect.  You will have to work out what problem is causing the error.  It is possible that whilst trying to correct the error you could actually make the code worse and then wish you could revert back to the code as it was.  Therefore, before changing the code, I recommend you copy the code and add apostrophes to each line of the copied version.  This will change the code into a comment.  If you ever need to revert back, the original code is right there.  Once the bug has been fixed completely, you can then delete the sections of old code, as they are no longer required.

 

Previous Part: 1.4 Understanding the code

Next Part: 1.6 Setting up the Visual Basic Editor environment

Save

Save

Save

Save

Leave a Reply

Your email address will not be published. Required fields are marked *