SECTION 3
Lesson 3.2: Visual Basic and Macros

   

 

 

Most good programmers will agree that adding comments to code is an important and often neglected practice. Well documented code is much easier to understand, repair, and modify than poorly documented code. Furthermore, experienced programmers will sometimes use comments to debug their code. For example, you can easily comment out a single line or several lines of code so that the computer will ignore them when the macro is played. This will eliminate them as a possible source of error, and show you how the macro runs without them.

 

To add a comment to a macro, open the visual basic window by choosing Tools->Macro->Macros. In the Macro dialogue box, select a macro and click the Edit button. The visual basic window will be displayed showing the visual basic code for the macro.

 

 

The statements in green beginning with an apostrophe (‘) are comments. When the macro is played, any lines beginning with an apostrophe (‘) will not be executed. In fact, they will be ignored completely. You can type pretty much anything you want after an apostrophe without creating an error in the macro. Clearly, comments are ideal for documenting visual basic code with information and hints as to what the code does.

 

The following lines taken from the code shown above are comments describing the name of  the macro, who recorded it, and when.

 

We can easily append helpful information to a line of code, by typing an apostrophe, and then a brief description of what the particular line of code does.

 

Here is an example.

 

 

To add this kind of comment to your macro, place your cursor at the end of the visual basic instruction that you want to elaborate on and type an apostrophe. Immediately following the apostrophe, add a description of what the particular line of visual basic code does and press Enter. Everything on the same line that comes after the apostrophe will turn green meaning that it is a comment. Only the green text in the macro will be ignored when you run it.

 

You can also place a comment on its own line, usually just above or sometimes just below the section of code it describes. To do this, just place your cursor on a new line above or below a statement or statements, and type an apostrophe followed by your comment. When you press Enter or an arrow key to move to the next line, what you typed will be recognized as a comment and turn green. If you think you need more than one line for a comment, take as many lines as you require, but be sure to begin each one with an apostrophe.

 

As an example, here is Macro6 with comments added.