Microsoft Office Tutorials and References
In Depth Information
Using Comments in Your Code
Using Comments in Your Code
A comment is descriptive text embedded within your code. VBA completely ignores the text of a
comment. It’s a good idea to use comments liberally to describe what you do because the
purpose of a particular VBA instruction is not always obvious.
You can use a complete line for your comment, or you can insert a comment after an instruction
on the same line. A comment is indicated by an apostrophe. VBA ignores any text that follows
an apostrophe up until the end of the line. An exception occurs when an apostrophe is contained
within quotation marks. For example, the following statement does not contain a comment, even
though it has an apostrophe:
Result = “That doesn’t compute”
The following example shows a VBA Function procedure with three comments:
Function MYFUNC()
‘ This function does nothing of value
x = 0 ‘x represents nothingness
‘ Return the result
MYFUNC = x
End Function
When developing a function, you may want to test it without including a particular statement or
group of statements. Instead of deleting the statement, simply convert it to a comment by
inserting an apostrophe at the beginning. VBA then ignores the statement(s) when the routine is
executed. To convert the comment back to a statement, delete the apostrophe.
The VB Editor Edit toolbar contains two very useful buttons. Select a group of
instructions and then use the Comment Block button to convert the instructions to comments.
The Uncomment Block button converts a group of comments back to instructions. If the
Edit toolbar is not visible, choose View
Toolbars
Edit.
Using Variables, Data Types, and Constants
A variable is a named storage location in your computer’s memory. Variables can accommodate
a wide variety of data types — from simple Boolean values (TRUE or FALSE) to large,
doubleprecision values (see the “Defining data types” section). You assign a value to a variable by using
the assignment operator, which is an equal sign.
The following are some examples of assignment statements that use various types of variables. The
variable names are to the left of the equal sign. Each statement assigns the value to the right of
the equal sign to the variable on the left.
 
Search JabSto ::




Custom Search