Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 140

Comments

Оглавление

A comment is descriptive text embedded in your code and ignored by VBA. It's a good idea to use comments liberally to describe what you're doing because an instruction's purpose isn't always obvious.

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. Here's an 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 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.

The following are a few general tips on making the best use of comments:

 Use comments to describe briefly the purpose of each procedure that you write.

 Use comments to describe changes you make to a procedure.

 Use comments to indicate you're using functions or constructs in an unusual or a nonstandard manner.

 Use comments to describe the purpose of variables so that you and other people can decipher otherwise cryptic names.

 Use comments to describe workarounds that you develop to overcome Excel bugs or limitations.

 Write comments while you code rather than afterward.

 When you've completed all coding, take some time to go back and tidy up your comments, removing any comments that are no longer needed and expanding on comments that may be incomplete or a bit too cryptic.

Excel 2019 Power Programming with VBA

Подняться наверх