Microsoft Office Tutorials and References
In Depth Information
Comments
You can use a complete line for your comment, or you can insert a comment after an instruction
on the same line. A comment is indicated by an apostrophe. VBA ignores any text that follows an
apostrophe — except when the apostrophe is contained within quotation marks — up until the
end of the line. For example, the following statement doesn’t contain a comment, even though it
has an apostrophe:
Msg = “Can’t continue”
The following example shows a VBA procedure with three comments:
Sub CommentDemo()
‘ This procedure does nothing of value
x = 0 ‘x represents nothingness
‘ Display the result
MsgBox x
End Sub
Although the apostrophe is the preferred comment indicator, you can also use the Rem keyword
to mark a line as a comment. For example:
Rem -- The next statement prompts the user for a filename
The Rem keyword (short for Remark ) is essentially a holdover from older versions of BASIC and is
included in VBA for the sake of compatibility. Unlike the apostrophe, Rem can be written only at
the beginning of a line, not on the same line as another instruction.
Using comments is definitely a good idea, but not all comments are equally beneficial.
To be useful, comments should convey information that’s not immediately obvious
from reading the code. Otherwise, you’re just chewing up valuable bytes and increasing
the size of your workbook.
Following are a few general tips on making the best use of comments:
h Use comments to describe briefly the purpose of each procedure that you write.
h Use comments to describe changes that you make to a procedure.
h Use comments to indicate that you’re using functions or constructs in an unusual or
nonstandard manner.
h Use comments to describe the purpose of variables so that you and other people can
decipher otherwise cryptic names.
 
Search JabSto ::




Custom Search