Skip to content

Read VBA as objects, properties and methods

Write VBA9 minPremium

Builds on Read and edit a macro in the VB Editor.

A finance assistant in Graz keeps the department budget in a workbook for each quarter, with a sheet for each month. At the start of a month she clears the last month's figures from the new sheet and writes its title, and she wants a macro for it.

How it works

VBA works on objects, the things you see in Excel, and they sit inside one another. Excel holds workbooks, a workbook holds worksheets, and a worksheet holds cells. A dot goes one level down, so a line names the cells it works on the way an address names a house.

LevelIn code
1ExcelApplication
2WorkbookWorkbooks("Q1.xlsm")
3WorksheetWorksheets("March")
4CellsRange("B4")

  1. Step 1: Excel itself, at the top, holds every open workbook.
  2. Step 2: One workbook, by its file name, holds its worksheets.
  3. Step 3: One sheet in that workbook, by its tab name, holds its cells.
  4. Step 4: One cell or block on that sheet: here B4.
Each dot goes one level down. Range("B4") on its own means B4 on the sheet you're looking at.

Leave out the outer levels and VBA uses the active ones: Range("B4") is B4 on the active sheet of the active workbook.

A property is something an object has, such as a cell's Value, NumberFormat or Interior.Color. Set it with an equals sign: Range("B4").Value = 2500. A method is something an object does, such as ClearContents or AutoFit. It takes no equals sign: Range("B5:B20").ClearContents.

Example

VBA
Sub StartMarch()
Worksheets("March").Range("C2:C25").ClearContents
Worksheets("March").Range("A1").Value = "Budget for March"
End Sub
Clears last month's figures on the March sheet and writes its title, without selecting the sheet.

To try a single line, press CtrlG in the editor to open the Immediate window, type the line and press Enter: VBA runs it at once. Start with a question mark to see a value, as in ?Range("B4").Value. On a Mac, choose View > Immediate Window.

Common mistakes

  • Range("A1:E1").Color = vbYellow. A fill color belongs to the cells' interior, so VBA stops with "Object doesn't support this property or method". Write Range("A1:E1").Interior.Color = vbYellow.
  • An equals sign after a method, as in Range("B5:B20").ClearContents = True. A method is an action, so it takes no equals sign: Range("B5:B20").ClearContents.
  • Text without quotes, as in Range("A1").Value = Budget. VBA reads Budget as the name of an empty variable and clears A1. Put text in double quotes.

In short

  • Objects sit inside one another, and a dot goes one level down: workbook, worksheet, range.
  • A property is set with =; a method runs without one.
  • Name the sheet, as in Worksheets("March").Range("A1"), to work on cells without selecting them.

Practice

Premium

4 exercises: 3 macros and 1 quiz question.

Practice is part of Premium

Every lesson is free to read, and you can practice the free lessons without an account. This lesson's exercises are part of Premium, which you add to a free account.