Two Ways to Specify Arguments of Methods
VBA methods usually take one or more arguments. The Sort method, for example, takes 10 arguments. The syntax of the Sort method is object.Sort(key1, orderl, key2, order2, key3, order3, header, orderCustom,
matchCase, orientation)
The object argument is required; all other arguments are optional.
You can specify the arguments of a method in two ways. One way is to list the arguments in order as they are specified in the preceding syntax, i.e.,
Range("A1:E150").Sort "Last Name", xIAscending which sorts the data contained in the range A1:E150 in ascending order, using as the sortkey the values in the column headed by the label Last Name. xIAscending is one of many built-in constants. You can look them up in the Online Help or use the Recorder to provide the correct one.
In the preceding example, only the arguments keyl and orderl were specified; the remaining arguments are optional and are not required.
The second way is to use the name of the argument as it appears in the preceding syntax, with the := operator, to specify the value of the argument, as in the following:
Selection.Sort Key1:=Range("A2"), Orderl :=xlAscending, _
Key2:=Range("B2"), Order2:=xlAscending, Key3:=Range("C2"), _
Order3:=xlDescending, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
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.
Post a comment