Microsoft Office Tutorials and References
In Depth Information
Notice that the first argument is omitted and I specified the placeholder using a comma.
Another approach, which makes your code more readable, is to use named arguments.
Here’s an example of how you use named arguments for the preceding example:
Workbooks(“MyBook.xls”).Protect Structure:=True, Windows:=False
Using named arguments is a good idea, especially for methods that have lots of
optional arguments, and also when you need to use only a few of them. When you use
named arguments, you don’t need to use a placeholder for missing arguments.
For properties that use arguments, you must place the arguments in parentheses. For
example, the Address property of a Range object takes five arguments, all of which
are optional. The following statement is not valid because the parentheses are omitted:
MsgBox Range(“A1”).Address False ‘ invalid
The proper syntax for such a statement requires parentheses, as follows:
Or the statement could also be written using a named argument:
These nuances will become clearer as you gain more experience with VBA.
With literally thousands of objects, properties, and methods at your disposal, how
do you learn about them all? There are three general ways:
Use the macro recorder (see the next section).
Use the Help system. To get specific help about a particular object,
property, or method, type the word in a VBA code window and press F1.
Figure 15-2 shows an example of the help screen for the Series object.
Notice that it contains links to display the properties and methods for
Study (or adapt) VBA code written by others. Chapter 16 is a good starting