Microsoft Office Tutorials and References
In Depth Information
An Introductory Example
The first line of the procedure is called the declaration line . Notice that the
procedure starts with the keyword Sub , followed by the name of the
procedure ( CountCharts ). This procedure uses no arguments, so it is followed
by a set of empty parentheses.
The second line is simply a comment (optional) that describes what the
procedure does. The initial apostrophe designates this line as a comment.
The next line uses the Dim keyword to declare a variable (named Msg ) and
its data type ( String ). Declaring variables isn’t necessary, but (as you’ll
see later) it’s an excellent practice.
The procedure’s next line assigns a value to the variable. It uses the Name
property of the ActiveSheet . At this point, the Msg variable contains the
name of the active sheet (such as Sheet1 ).
The next statement uses the concatenation operator (&) to add a “new
line” character to the end of the current contents of the Msg variable.
The next statement counts the number of chart objects on the active sheet,
and this value is appended to the contents of the Msg variable, again
using the concatenation operator.
The next statement uses VBA’s MsgBox function to display the contents of
the Msg variable. This appears in the form of a pop-up dialog box that the
user must respond to.
The procedure ends with an End Sub statement.
This simple macro uses some common VBA language elements, including:
A comment (the line preceded by the apostrophe)
A variable declaration
Three assignment statements
String concatenation (using the & operator)
References to properties ( Name and Count )
The MsgBox function
Not bad for a first effort, eh? The remainder of this chapter provides more
information on these (and many other) programming concepts.
Search JabSto ::

Custom Search