Visual Basic for Applications is a derivative of the programming language Visual Basic. Each Office application has its own particular kind of VBA depending on the objects and operation of the application. For example, Microsoft uses worksheets while Microsoft Word works with documents. The following notes explain some of the key concepts in VBA programming.
Object Orientated and Procedure Driven
Visual Basic for Applications and Visual Basic are both object-orientated programming languages because they work with objects. Most of these objects appear on the screen, hence the term ‘visual’. They are also procedure-driven languages using commands and structures from the BASIC programming language to bind object statements into workable applications.
Objects, Properties, Methods and Events
In VBA an object is anything in an application that you can see and manipulate in some way. For example, you can manipulate a worksheet by adding rows, deleting columns, displaying gridlines, and so on. A worksheet is therefore an example of an object. Rows are also objects, as are columns too. These are child objects of the parent object – the worksheet. This way of organizing objects into a hierarchy is known as an object model.
Manipulate objects 1 of 3 ways:
- Change the way an object looks or behaves by changing its properties
- Make an object perform a task by using a method that is associated with the object
- Run a procedure whenever a particular event happens to an object=
Objects therefore have properties, methods and events.
Real World Example….
Let’s look at a simple real-world analogy to get a better idea about objects, properties, methods and events. Consider a car: it is an object because you can see it and manipulate it. Its:
- Properties are its physical characteristics such as its make, model, colour and so on
- Methods define what you can do with the car such as reversing, accelerating, turning, stopping and so on
- Events are the actions that happen to the car that generate an automatic response from the car. For example, if you remove the keys from the ignition while the car’s headlights are on (event), most cars will sound a warning alarm or turn off the lights (response).
The Active Object
In VBA, active describes the object item that you’re currently working on.
For example, the worksheet cell that you’re editing or formatting in Excel is the active cell. The workbook that you are currently working on is said to be the active workbook. The object that is currently active is said to have the focus.
This is an important concept to understand because most of your VBA programming will be performing an action on a particular object. If you don’t identify that object correctly, you may find that Excel shifts focus behind the scenes to a different object and your program will fail.
If you would like to learn more about our Excel VBA courses click here.