Commenting Your Code

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 and a human.

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 as you go — to remind yourself of the purpose of different parts of your code

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 doesn't treat it as code. When viewing existing code, you see the apostrophe at the start of each comment within the code, as shown in the example in Figure 4-1. (In the Code window, comments are also colored green.)

Figure 4-1:

Add comments to make your code understandable.

Figure 4-1:

Add comments to make your code understandable.

Write Note Vba Window
Comments start with an apostrophe.

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

As we 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. One of the first things you need to understand is that, like tables in Access, VBA has data types.

0 0

Post a comment