Wednesday, September 26, 2012

Excel VBA - Objects, Properties & Methods


In Excel VBA, objects, properties and methods are connected with a dot. Properties are something which an object has (they describe the object), while methods do something (they perform an action with an object).

Excel VBA Property


Let's take a look at an Excel VBA object and an Excel VBA property. We will use the Range object and the Formula property. The Range object is nothing more than a cell (or cells) on your worksheet. We already know from Excel that a cell can contain a formula.

1. Place the value 10 into cell A1.
2. Create a command button.
3. Add the line:
Range("B1").Formula = Range("A1") * 2

4. Execute the macro. This macro places the formula into cell B1 and the result will be calculated (20).


Excel VBA Method


Now let's take a look at an Excel VBA object and an Excel VBA method. We will use the Range object again and the ClearContents method.
1. Place the value 10 into cell A1.
2. Create a command button.
3. Add the line:
Range("A1").ClearContents

4. Execute the macro. Cell A1 is now empty!
That's how easy object-oriented programming in Excel Visual Basic is!

See all the properties and methods of an object.


The Range object has many more properties and methods. Want to see more?
1. Launch the Visual Basic Editor.
2. Type in: Range.
3. A list will appear showing you all the Excel VBA methods and properties of the Range object. The fingers are the properties and the green boxes are the methods of the Range object.
All properties and methods of the Range object

No comments:

Post a Comment