Com ments
Starting
in this chap ter, our ex amples are go ing to get a little more compli-
cated. That makes this a good time to talk about comments. So far, any explana-
tion of the VBA code
has been placed in the text, but it might be helpful to have
some ex planation built right into the code. This will be useful for me to ex plain
things to you, and it will be useful for you to ex plain things to yourself. Com-
ments are good at the
beginning of pro cedures, as a brief note at the end of a line,
and as a note in side a pro cedure. In ad dition, comments
are helpful to point out
obvious things be cause what is ob vious to me might not be ob vious to you, and
what is ob vious to you now might not make as much sense when you look at it
later. And comments are helpful to point out things that are not obvious.
A line
like
If an
swer = "" Then
obviously checks to see if the variable
an
swer
con tains noth ing, but it might be
helpful
to put a com ment, such as “The user did n’t type any thing.”
If an
swer = "" Then 'The user did
n’t type any
thing.
The comment starts with a single quote. This tells the
computer to ig nore every-
thing else on the line. That is, comments are for people look ing at VBA code, not
for computers run ning VBA code; the computer ignores the comments. As in
this example, the com ment can ap pear
at the end of a line, or it can ap pear on a
line by itself or even on sev eral lines each starting with a single quote:
'This pro
cedure is our very first pro
cedure.
'It puts a mes
sage on the user’s screen that says "Hello."
Sub SayHello()
MsgBox("Hello") 'This is the line that puts up the mes
Do'stlaringiz bilan baham: