Object methods

In addition to properties, objects have methods. A method is an action you perform with an object. A method can change an object's properties or make the object do something.

This simple example uses the Calculate method on a Range object to calculate the formula in cell A1 on Sheet1:

Sub CalcCell()

Worksheets("Sheet1").Range("A1").Calculate End Sub

Most methods also take one or more arguments. An argument is a value that further specifies the action to perform. You place the arguments for a method after the method, separated by a space. Multiple arguments are separated by a comma.

The following example activates Sheet1 (in the active workbook) and then copies the contents of cell A1 to cell B1 using the Range object's Copy method. In this example, the Copy method has one argument — the destination range for the copy operation:

Sub CopyOne()

Worksheets("Sheet1").Activate Range("A1").Copy Range("B1") End Sub

Notice that I omit the worksheet reference when I refer to the Range objects. I could do this safely because I used a statement to activate Sheet1 (using the Activate method).

Because a collection is also an object, collections have methods. The following macro uses the Add method for the Workbooks collection:

0 0

Post a comment