Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Lesson 16: Mastering Excel VBA Objects

Learn how to work with objects in Excel VBA to create powerful automation scripts


16.1 Object-Oriented Programming in Excel VBA

Excel VBA 365 is an object-oriented programming language that allows you to interact with Excel's components programmatically. Understanding objects is crucial for effective Excel automation as everything in Excel VBA revolves around objects and their properties/methods.

Key concepts of object-oriented programming in Excel VBA:

To view all available Excel VBA objects, click the Object Browser in the VBA Editor (F2 shortcut) where you'll see a comprehensive list of objects with their properties and methods, as shown in Figure 16.1.

Excel VBA Object Browser
Figure 16.1: Excel VBA Object Browser showing available objects and their members

When working with UserForms, clicking on controls reveals their associated objects and events, as shown in Figure 16.2.

UserForm objects and events
Figure 16.2: UserForm objects and their associated events

16.2 Understanding Object Properties

Properties define the characteristics or attributes of an object. In Excel VBA, you access an object's properties using dot notation (object.property). The Range object, one of the most frequently used objects, has properties like Value, Font, Interior, and more.

Practical Example 16.1: Working with Range Properties

Private Sub CommandButton1_Click()
    ' Set value for range A1:A6
    Range("A1:A6").Value = 10
    
    ' Alternative syntax (Value is default property)
    Range("A1:A6") = 10
End Sub

Pro Tip: The Value property is the default property for Range objects, so you can omit it for cleaner code.

Object Hierarchy in Excel VBA

Objects exist in a hierarchy where some objects are properties of other objects. For example:

This hierarchy allows for precise control over Excel elements:

' Color only the first cell in range A1:A3
Range("A1:A3").Cells(1, 1).Interior.Color = vbYellow

' Color all cells in range A1:A3
Range("A1:A3").Interior.Color = vbYellow

' Change font color for range A1:A4
Range("A1:A4").Font.Color = vbRed

Using IntelliSense for Faster Coding

Excel VBA's IntelliSense feature helps you discover properties and methods as you type. After typing an object name followed by a period (.), a dropdown appears showing available members (Figure 16.3). This eliminates memorization and reduces errors.

VBA IntelliSense in action
Figure 16.3: IntelliSense showing available properties and methods

16.3 Practical Applications of Excel VBA Objects

Here are some common object manipulations with practical examples:

Working with Worksheets

' Add a new worksheet
Worksheets.Add.Name = "SalesData"

' Protect a worksheet
Worksheets("Sheet1").Protect Password:="mypassword"

' Change tab color
Worksheets("Sheet1").Tab.Color = RGB(255, 0, 0)

Managing Workbooks

' Save all open workbooks
For Each wb In Application.Workbooks
    wb.Save
Next wb

' Close workbook without saving
Workbooks("Report.xlsx").Close SaveChanges:=False

Advanced Range Operations

' Format a range as currency
Range("B2:B20").NumberFormat = "$#,##0.00"

' AutoFit columns
Range("A1:D1").Columns.AutoFit

' Add borders
With Range("A1:D10").Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 1
End With

Best Practices When Working with Objects

  1. Always qualify objects fully (e.g., Workbooks("Data.xlsx").Worksheets("Sheet1"))
  2. Use the With statement when making multiple property changes to the same object
  3. Release object references when done by setting them to Nothing
  4. Handle errors when working with objects that might not exist

Key Takeaways: Excel VBA Objects

  • Excel VBA is object-oriented - everything is an object with properties and methods
  • Use the Object Browser (F2) to explore available objects and their members
  • Access properties and methods using dot notation (object.property)
  • Objects exist in a hierarchy (Application → Workbook → Worksheet → Range)
  • IntelliSense helps discover properties/methods as you type
  • The Range object is one of the most frequently used objects in Excel VBA
  • Use With statements to make multiple changes to the same object efficiently

🔗 Related Resources


Ready for More?

In the next lesson, we'll explore Excel VBA Objects - the building blocks of VBA programming that let you control every aspect of Excel.


Copyright ® 2023 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page