Comments

Tags:

A comment is descriptive text embedded within your code. The text of a comment is completely ignored by VBA. It’s a good idea to use comments liberally to describe what you’re doing.

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 does not contain a comment, even though it has an apostrophe:

Msg = “Can’t continue”

The following example shows a VBA procedure with three comments:

Sub Comments()
    ‘ 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 is essentially a holdover from old versions of BASIC; it 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.