Adding Comments

Take a look at the recorded macro code. Notice the lines that begin with an apostrophe. These lines indicate comments. By default, comments appear in green. When the macro code is executed, Visual Basic ignores the comment lines. Comments are often placed within the macro code for documenting the meaning of certain lines that aren't obvious. Let's now add some comments to the WhatsInACell macro.

1. Activate the Visual Basic Editor window.

2. Click in front of Selection.SpecialCells(xlCellTypeConstants, 2).Select and press Enter.

3. Move the pointer to the empty line above and add the comment shown below. Be sure to start with an apostrophe.

' Find and format cells containing text

4. Click in front of Selection.SpecialCells(xlCellTypeConstants, 1).Select and press Enter.

5. Move the pointer to the empty line above and add the following comment:

' Find and format cells containing numbers

6. Click in front of Selection.SpecialCells(xlCellTypeFormulas, 23).Select and press Enter.

7. Move the pointer to the empty line above and add the following comment:

' Find and format cells containing formulas

8. Click in front of Range("A1:A3").Select and press Enter.

Tip 1-6: About Comments

■ In the VBE Code window, every line that begins with an apostrophe is a comment. The default comment color is green. You can change the color of comments in the Options dialog box (Tools | Options | Editor Format tab).

■ You can also add a comment at the end of the line of code. For example, to add a comment following the line .ColorIndex =11, click at the end of this line, press Tab, enter the apostrophe, and then type the text of your comment. When you are done, the commented line should look as follows:

.ColorIndex = 11

Sets the font color to Violet

The comment lines don't do anything except provide information to the user about the purpose of a macro or macro action. When you write your own VBA procedures, don't forget to include comments. Comments will make your life easier if you need to return to the macro procedure several months later. They will also allow others to quickly understand various parts of your procedure.

9. Move the pointer to the empty line above and add the following comment:

' Create legend

0 0

Post a comment