How to Edit VBA Code in Excel
Access, modify, and manage your VBA macros with the Visual Basic Editor
Quick Answer
Open VBA Editor: Press Alt + F11 (Windows) or Option + F11 (Mac) to open the Visual Basic Editor.
Find your code: In the Project Explorer, expand your workbook → double-click Modules → select your module to view and edit code.
Opening the VBA Editor
Method 1: Keyboard Shortcut (Fastest)
Windows
Alt + F11Mac
Option + F11Or Fn + Option + F11
Method 2: Developer Tab
- Go to the Developer tab on the ribbon
- Click Visual Basic button
- VBA Editor opens in a new window
Don't see Developer tab? File → Options → Customize Ribbon → Check "Developer" → OK
Method 3: From Macros Dialog
- Press Alt + F8 to open Macros dialog
- Select a macro from the list
- Click Edit button
- VBA Editor opens with that macro's code displayed
Understanding the VBA Editor Interface
Main Components:
Shows all open workbooks, their sheets, and code modules. This is where you navigate your VBA projects.
The main editing area where you write and modify VBA code. Can have multiple code windows open simultaneously.
Displays properties of selected objects (UserForms, sheets, modules). Change object names and settings here.
Test code snippets and debug. Type ? followed by expression to evaluate it immediately.
Access VBA tools, debugging features, and editor preferences.
Pro Tip: Use View menu to show/hide different windows. Customize your workspace for maximum productivity!
Finding Your VBA Code
Code Can Be Stored In:
📄 Modules (Most Common)
General-purpose code, functions, and subroutines
Location: VBAProject → Modules → Module1, Module2, etc.
📊 Worksheet Objects
Sheet-specific code (Worksheet_Change, Worksheet_SelectionChange events)
Location: VBAProject → Microsoft Excel Objects → Sheet1, Sheet2, etc.
📗 ThisWorkbook
Workbook-level events (Workbook_Open, Workbook_BeforeClose)
Location: VBAProject → Microsoft Excel Objects → ThisWorkbook
🎨 UserForms
Custom dialog boxes with controls and event handlers
Location: VBAProject → Forms → UserForm1, etc.
📚 Class Modules
Custom objects with properties and methods
Location: VBAProject → Class Modules → Class1, etc.
Quick Navigation Tips:
- Ctrl+R: Show/hide Project Explorer
- Ctrl+F: Find text in code
- Ctrl+H: Find and replace
- F2: Open Object Browser (see all available objects and methods)
Editing VBA Code
Basic Editing:
- Double-click any module in Project Explorer to open it
- Click in the code window to start editing
- Type or modify code just like a text editor
- VBA auto-formats code when you press Enter (capitalizes keywords, adds spaces)
- Save changes with Ctrl+S
Editor Features:
- ✓ Auto List Members: Press Ctrl+J to see available properties/methods for an object
- ✓ Auto Quick Info: Shows function parameters as you type
- ✓ Auto Complete Word: Ctrl+Space to complete partially typed keywords
- ✓ Comment Block: Select lines → click Comment Block button (or Ctrl+Shift+C in some versions)
- ✓ Indent/Outdent: Tab to indent, Shift+Tab to outdent
- ✓ Syntax Checking: VBA checks syntax when you press Enter
Important: Changes in VBA Editor are saved to the workbook. Remember to save your Excel file to persist code changes!
Creating New Code Modules
Insert a New Module:
- In VBA Editor, click Insert menu
- Select Module
- A new Module appears in Project Explorer
- Double-click to open and start coding
Create a New Subroutine:
Type Sub followed by the macro name and parentheses. Press Enter and VBA automatically adds End Sub.
Create a New Function:
Functions return values and can be used in worksheet formulas or other VBA code.
Testing Your Code
Run Code:
- • F5: Run current subroutine (cursor must be inside the Sub)
- • F8: Step through code line by line (debugging)
- • Ctrl+Break: Stop running code
- • Run → Run Sub/UserForm: Same as F5
Use Immediate Window:
? Range("A1").Value
Press Enter to see A1's value instantly
Press Ctrl+G to open Immediate Window for quick testing.
Debugging Tip: Use Debug.Print to output values to Immediate Window while code runs.
Best Practices for Editing VBA
CalculateTotalSales not Macro1Dim statements with data typesCommon Editing Tasks
Delete a Macro/Module:
Right-click module in Project Explorer → Remove ModuleName → No (don't export) or Yes (save backup)
Rename a Module:
Click module → In Properties Window (F4) → Change (Name) property
Export Code:
Right-click module → Export File → Save as .bas file for backup/sharing
Import Code:
File → Import File → Select .bas file to add to project
Copy Code Between Workbooks:
Open both workbooks → Open VBA Editor → Drag module from one project to another in Project Explorer