Commenting Your Code

xjweh

When you look at existing code, notice that some lines look like plain English while others look like VBA code. The lines that look like English are programmer comments. Only humans see comments; the computer sees only the VBA code. Thus, using comments is entirely optional to you, as a programmer.

Programmers add comments to their code for two reasons:

I To help others who are trying to understand how the code works.

I To jot down notes to yourself as you go — to remind yourself of the purpose of different parts of your code.

When typing your own code, you're welcome to type in your own comments. They don't have to be written for other programmers.

The first character of a comment must be an apostrophe (' ). The comment ends where you press Enter to end the line. After you type the apostrophe, you can type any text you want on that same line because VBA won't treat it as code. When viewing existing code, you'll see the apostrophe at the start of each comment within the code, as in the example shown in Figure 4-1. (In the Code window, comments are also colored green.)

Figure 4-1:

Add comments to make your code clear.

Comments

Figure 4-1:

Add comments to make your code clear.

Comments

When you're modifying existing code, remember that the comments are for human consumption only. Changing a comment won't fix code or change how it works at all. Comments are only notes jotted down within VBA code.

As I mention, writing comments is easy because after you type the initial apostrophe, you can type anything you want. Writing code, though, is a lot harder because of the rules of syntax (word order and punctuation). Plus, there are lots of rules concerning the data on which VBA can operate. Like when learning to create tables in Access, one of the first things that you need to understand is that like tables in Access, VBA has data types.

0 0

Post a comment