Comment Your Code

You can provide your reader an explanation of how you intend your code to function by commenting your code. A comment adds descriptive text to your code that provides the reader some sense of the functionality of the referenced code.

Whenever a subroutine or function executes VBA ignores comment lines, so you can and should use them liberally. It may seem monotonous to describe your code today, because you already know what it does. However, comments help you quickly determine code functionality, especially a few months from now when you come back to work with the code again. Of course, comments only help if they provide enough information to describe the code. A reader should be able to read the comments only, without studying the code, and get a good sense of what the code does. For example, a comment like "sums the values" does not provide any information about the code other than stating that the code adds some values together. A better comment is "Sums the values in cells A1 and A2 and places the result in cell A3" because it describes the actual process.

You can add comments as entire lines of text or place them at the end of a line of code. To indicate a comment, type an apostrophe at the beginning of the comment line. VBA ignores all text from the apostrophe to the end of the line.

The only time that VBA does not treat an apostrophe as a comment is when you type the apostrophe within quotation marks as part of a string of text. For example, VBA does not treat the following statement as a comment:

COMMENT YOUR CODE

COMMENT YOUR CODE

"I ThlsWcrkbook

Module2 L-«<t Module3 E-Îé VBAProject (OfficeEx

Module3 Module "-1

Alphabetic | categorized |

ie) Module3

-D In the Project window, double-click to select the module that contains the procedure you want to document.

■ The selected module code displays in a Code window.

"I ThlsWcrkbook

Module2 L-«<t Module3 E-Îé VBAProject (OfficeEx

Const NewConstant As HsgBox (NeraConstant) End Sub

Module3 Module "-1

Alphabetic | categorized |

ie) Module3

Const NewConstant As HsgBox (NeraConstant) End Sub

1HÎ1EJ

asi.n

-D In the Project window, double-click to select the module that contains the procedure you want to document.

■ The selected module code displays in a Code window.

H Place the cursor at the end of the procedure declaration line.

Excel inserts a blank line.

0 0

Post a comment