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
- Insert › Module – creates
Module1. General macros and functions go in standard modules. - At the top of every module type
Option Explicit(forces you to declare variables). Make it automatic: Tools › Options › Editor › tick Require Variable Declaration. - Rename a module: select it › F4 › change (Name) to
modReports. - Export/import modules (right-click) to reuse code in other workbooks.
- In the Immediate window type
?Worksheets.Countand 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.
Ravindra Bagale's Tip – मराठी
Code sheet च्या module मध्ये (उदाहरणार्थ Sheet1 (Orders)) लिहिला तर तो फक्त त्या sheet साठी चालतो आणि function worksheet मध्ये सापडत नाही – बरेच students इथे अडकतात. Normal macros आणि UDF नेहमी Insert › Module मध्ये लिहा. आणि Option Explicit शिवाय code लिहू नका – spelling ची चूक लगेच पकडली जाते.
Ravindra Bagale's Tip – हिंदी
Code को sheet के module में (जैसे Sheet1 (Orders)) लिखा तो वह सिर्फ़ उसी sheet के लिए चलता है और function worksheet में नहीं मिलता – बहुत से students यहाँ अटक जाते हैं. Normal macros और UDF हमेशा Insert › Module में लिखो. और Option Explicit के बिना code मत लिखो – spelling की गलती तुरंत पकड़ी जाती है.
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.