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 + F11

Mac

Option + F11

Or Fn + Option + F11

Method 2: Developer Tab

  1. Go to the Developer tab on the ribbon
  2. Click Visual Basic button
  3. VBA Editor opens in a new window

Don't see Developer tab? File → Options → Customize Ribbon → Check "Developer" → OK

Method 3: From Macros Dialog

  1. Press Alt + F8 to open Macros dialog
  2. Select a macro from the list
  3. Click Edit button
  4. VBA Editor opens with that macro's code displayed

Understanding the VBA Editor Interface

Main Components:

1. Project Explorer (Ctrl+R):

Shows all open workbooks, their sheets, and code modules. This is where you navigate your VBA projects.

2. Code Window:

The main editing area where you write and modify VBA code. Can have multiple code windows open simultaneously.

3. Properties Window (F4):

Displays properties of selected objects (UserForms, sheets, modules). Change object names and settings here.

4. Immediate Window (Ctrl+G):

Test code snippets and debug. Type ? followed by expression to evaluate it immediately.

5. Menu Bar & Toolbar:

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:

  1. Double-click any module in Project Explorer to open it
  2. Click in the code window to start editing
  3. Type or modify code just like a text editor
  4. VBA auto-formats code when you press Enter (capitalizes keywords, adds spaces)
  5. 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:

  1. In VBA Editor, click Insert menu
  2. Select Module
  3. A new Module appears in Project Explorer
  4. Double-click to open and start coding

Create a New Subroutine:

Sub MyMacroName()
' Your code here
MsgBox "Hello, World!"
End Sub

Type Sub followed by the macro name and parentheses. Press Enter and VBA automatically adds End Sub.

Create a New Function:

Function AddNumbers(num1 As Double, num2 As Double) As Double
AddNumbers = num1 + num2
End 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

Use comments liberally: Start comments with apostrophe ('). Explain what complex code does.
Indent your code: Makes structure clear and easier to read
Use meaningful names: CalculateTotalSales not Macro1
Declare variables: Use Dim statements with data types
Option Explicit: Add at top of modules to force variable declaration
Save frequently: Ctrl+S to save VBA changes to workbook
Test incrementally: Run code after small changes to catch errors early
Back up your work: Copy code to text file or version control

Common 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

Related VBA Tutorials

Generate VBA Code Automatically

Skip the manual coding - generate clean, documented VBA code with AI

✓ No credit card required ✓ 5 free generations