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

Code sheet chya module madhe (udaharanartha Sheet1 (Orders)) lihila tar to fakt tya sheet sathi chalto aani function worksheet madhe sapdat nahi – khup students ithe atakatat. Normal macros aani UDF nehmi Insert › Module madhe liha. Aani Option Explicit shivay code lihu naka – spelling chuk lagech pakadli jaate.

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.