Setting and Getting Properties

With most properties, you can either set or return their values; these are called read-write properties. With other properties, you can only return their values; these properties are called read-only properties. Each property description in Help tells you whether a property is read-write or read-only.

Setting a Property Value

For an assignment statement to set the value of a property, the expression that contains the property must appear on the left side of the equal sign. For example, to set the Value property of the Range object that refers to cell A1 on Sheet1, you would write the following code.


Worksheets("Sheet1").Range("A1").Value = 3.14159

You can also set a property value to another property value, as shown in the following code. This example copies the value from cell B2 on Sheet2 to cell A1 on Sheet1.


Worksheets("Sheet1").Range("A1").Value = _
    Worksheets("Sheet2").Range("B2").Value 

Getting a Property Value

For an assignment statement to get the value of a property, the expression that contains the property must appear on the right side of the equal sign. A variable typically appears on the left side of the equal sign.

For example, the following code gets the Value property of the Range object that refers to cell A1 on Sheet1 and assigns it to the variable myValue.


myValue = Worksheets("Sheet1").Range("A1").Value

You can also get a property value without assigning the property to a variable. For example, the following code gets the Value property of the Range object that refers to cell A1 on Sheet1 and then passes this value to the MsgBox function.


MsgBox Worksheets("Sheet1").Range("A1").Value

Using Built-in Constants as Property Values

Many properties have built-in constants as their values. For example, you can set the HorizontalAlignment property to xlCenter, xlLeft, xlRight, and so on. The Help topic for any given property contains a list of built-in constants you can use. You should always use the built-in constant, not the value that the constant represents, because the value may change in future versions of Visual Basic.