Microsoft Office Tutorials and References
In Depth Information
Properties and Methods
Most of the time, however, you can omit the Application object in your
references (it is assumed). If the Book1 object is the active workbook, you can even omit
that object reference and use this:
Worksheets(“Sheet1”).Range(“A1”)
And — I think you know where I’m going with this — if Sheet1 is the active
worksheet, you can use an even simpler expression:
Range(“A1”)
Contrary to what you might expect, Excel does not have a Cell object that
refers to an individual cell. A single cell is simply a Range object that
happens to consist of just one element.
Simply referring to objects (as in these examples) doesn’t do anything. To
perform anything meaningful, you must read or modify an object’s properties, or
specify a method to be used with an object.
Properties and Methods
This section discusses two key attributes of objects: their properties and their methods.
Object properties
Every object has properties, which you can think of as attributes. For example, a
Chart object has a property called HasTitle . This property will be True if the chart
contains a title, or False if it does not. You can write VBA code to display the value
of the HasTitle property or write VBA code to set the HasTitle property to True
or False.
You refer to a property in your VBA code by placing a period and the property
name after the object’s name. Here’s a procedure that uses VBA’s MsgBox function
to display the HasTitle property of the chart on the chart sheet named Chart1.
Sub DoesChartHaveTitle()
Msgbox Charts(“Chart1”).HasTitle
End Sub
The code in the preceding example displays the current setting of the HasTitle
property of the chart on the chart sheet named Chart1. Note that if this particular
chart sheet does not exist when this macro is executed, it will generate an error. In
other words, this simple procedure contains no error-checking code.
Search JabSto ::




Custom Search