Page 56 - Excel for Scientists and Engineers: Numerical Methods
P. 56
CHAPTER 2 FUNDAMENTALS OF PROGRAMMMG WITH VBA 33
When using this method, the arguments can appear in any order, and
optional ones can be omitted if you do not need to specify a value.
Arguments with or without Parentheses
The arguments of a method sometimes appear within parentheses, sometimes
without parentheses (see the examples immediately preceding). Sometimes
either syntax will work, sometimes one or the other fails. Why is this?
As well as performing an action, methods create a return value. The return
value can be either True or False: True means the method worked, False means
that it failed. Even the Chartwizard method creates a return value: True if the
chart was created successfully, False if the method failed. Usually you aren't
interested in these return values; if your procedure executed successfully, you
are happy. But occasionally the return value is important.
An example of a method that creates a useful return value is the
Checkspelling method. The Checkspelling method has the following syntax:
Application.CheckSpeIling(word)
If you use this method, you'll need the return value (either True or False) to
determine whether the word is spelled correctly.
If you want to use the return value of a method, you must enclose the
arguments of the method in parentheses. If the arguments are not enclosed in
parentheses, then the return value will not be available for use. Put another way,
the expression
result = Application.CheckSpeIling(ActiveCell.Value)
does not produce a syntax error, while the expression
result = Application.CheckSpelling ActiveCell.Value
does give a syntax error.
Making a Reference to a Cell or a Range
One of the most important skills you'll need in order to create Sub
procedures that manipulate data in workbooks is the ability to make a reference
to a cell or range of cells. You'll need to be able to send values from a worksheet
to a module sheet so that you can perform operations on the worksheet data, and
you'll need to be able to send the results back from the module sheet to the
worksheet.
A Reference to the Active Cell or a Selected Range
Often a macro will be designed to operate on a user-selected cell or range.