Microsoft Office Tutorials and References
In Depth Information
Using Comments in Your Code
About the MsgBox Function
Many of the examples in this chapter use VBA’s MsgBox function. One of the uses for
this function is to display a message to the user in a pop-up box. This is often useful
for displaying the value of a variable at various points throughout your code.
For more details on the MsgBox function, consult VBA’s help.
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.
Following are examples of comments:
‘ Close the workbook without saving the changes
ActiveChart.Parent.Delete ‘ Delete the Chart object
VBA ignores any text that follows an apostrophe through to the end of the line.
An exception occurs when an apostrophe is contained within quotation marks. For
example, the following statement doesn’t contain a comment, even though it has
an apostrophe:
Result = “Can’t create a chart.”
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, double-precision values. You assign a value to a variable by using
the assignment operator, which is an equal sign.
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.
x = 1
SeriesCount = 4
Search JabSto ::




Custom Search