Ravindra BagaleCourses & study guides

12. Macros and VBA

12.4 The VBA Editor (VBE)

Open with Alt + F11 (or Developer › Code › Visual Basic).

Part Open with Purpose
Project Explorer Ctrl + R Tree of open workbooks: Microsoft Excel Objects (sheets, ThisWorkbook), Modules, Forms
Properties window F4 Properties of the selected object (e.g. a sheet's Name/CodeName)
Code window double-click a module Where you write code
Immediate window Ctrl + G Run one line or print values: ?Range("A1").Value, Debug.Print output
Locals / Watch windows View menu See variable values while debugging (12.15)

Steps in the VBE

  1. Insert › Module – creates Module1. General macros and functions go in standard modules.
  2. At the top of every module type Option Explicit (forces you to declare variables). Make it automatic: Tools › Options › Editor › tick Require Variable Declaration.
  3. Rename a module: select it › F4 › change (Name) to modReports.
  4. Export/import modules (right-click) to reuse code in other workbooks.
  5. In the Immediate window type ?Worksheets.Count and press Enter to see the number of sheets.

Ravindra Bagale's Tip

If you write code in a sheet's module (for example Sheet1 (Orders)), it works only for that sheet and the function can't be found on the worksheet – many students get stuck here. Always write normal macros and UDFs in Insert › Module. And don't write code without Option Explicit – spelling mistakes get caught immediately.

Practice task

Insert a module named modPractice with Option Explicit. In the Immediate window print the name of the active sheet (?ActiveSheet.Name) and the value of A1.