USING COMMENTS IN YOUR VBA CODE
Comments should be used in a program to describe what the code does.
Comments are ignored by VBA. They are used for the benefit of the programmer and
for future programmers who need to understand the code for maintenance.
An apostrophe marks the beginning of a comment.
VBA ignores any text that follows an apostrophe in a line of code. For example:
Sub firstProgram( )
' This is my first VBA program.
' This sub procedure prints out the "Hello world!" message.
' K. Ming Leung, 10/19/2006
Message = "Hello world!" 'variable containing a string
MsgBox Message
End Sub
VBA does not interpret an apostrophe inside a set of quotation marks (double quote) as a comment indicator.
For example:
Message = "Can't buy me love."
When testing a procedure you may want to remove some statements temporarily. Rather than deleting the statement, you can convert them into comments.
Then when the test is done, convert them back to statements.
This is called commenting and uncommenting out some statements.
In the VBA editor, choose View/Toolbars/Edit to display the Edit toolbar.
To convert a block of statements to comments, select the statements and click the Comment Block button.
To remove the apostrophes, select the statements and click the Uncomment Block button.
Comments should be used to:
-
briefly describe the purpose of each Sub or Function procedure.
-
keep track of changes or updates made to a procedure, including dates and the purposes for those changes.
-
describe the variables used (especially if you don't use meaningful variable names).
-
to describe the code as it is developed, rather than saving the task for a final step.