Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

The VBA Editor and Macro Security

Turn on the Developer tab, open the Visual Basic Editor, learn its windows, and understand macro security, trusted locations and safe file types.

Before writing code you need to find the tools. VBA is edited in the Visual Basic Editor (VBE), a separate window that opens on top of Excel, Word or any other Office app. This lesson shows how to open it, what each part does and how to keep macros safe.

Show the Developer tab

The Developer tab is hidden by default.

  1. Choose File > Options > Customize Ribbon.
  2. In the right list tick Developer.
  3. Click OK.

The tab holds the buttons Visual Basic, Macros, Record Macro, Macro Security and the form controls. You can also open the editor at any time with Alt + F11, and the Macros dialog with Alt + F8.

Tour of the editor

The editor windows

Project Explorer Workbooks, sheets, modules. Ctrl+R.
Code window Where you type. F7 opens it.
Properties Names and settings. F4.
Immediate window Test one line. Ctrl+G.
1 / 2
  • Project Explorer lists every open workbook as a project. Under each: Microsoft Excel Objects (the workbook and each sheet), Forms, Modules, Class Modules.
  • Code window shows the code of the selected item. Two drop-downs at the top choose the object and the event or procedure.
  • Properties window edits names, for example rename Module1 to modReports.
  • Immediate window runs a single statement instantly. Type ? 2 + 3 and press Enter to print 5. Type Debug.Print "hi" in code and the text appears there too.

Insert your first module

Right-click the project, choose Insert > Module. A blank code window appears. Standard procedures go into standard modules like this. Code for a particular sheet or workbook goes into that object's own code window (used for events, see Events later in the course).

Useful editor settings

Open Tools > Options:

  • Require Variable Declaration: tick it. The editor then adds Option Explicit to every new module (see Option Explicit).
  • Auto Syntax Check: consider turning it off if the pop-up errors while you type annoy you; the line still turns red.
  • Auto List Members and Auto Quick Info: keep on. They suggest properties and methods as you type a dot.

Handy shortcuts: F5 run, F8 step through line by line, F9 toggle a breakpoint, Ctrl+Space autocomplete, Ctrl+Z undo in the editor.

Macro security

A macro can do anything you can do, including deleting files. Office protects you with several layers:

Setting Meaning
Disable all macros without notification Nothing runs, no prompts
Disable with notification (default) A yellow bar asks you to enable content
Disable except digitally signed macros Only trusted publishers run
Enable all macros Dangerous, never use it

You find them in File > Options > Trust Center > Trust Center Settings > Macro Settings.

Two more protections matter:

  • Mark of the Web: files downloaded from the internet or opened from email are blocked from running macros until you unblock them (file Properties > Unblock). Do this only for files you trust.
  • Trusted Locations: workbooks in a folder you mark as trusted open with macros enabled. Choose a private folder that only you can write to, never Downloads or a shared drive everyone uses.

Never enable macros in a file from an unknown sender. Malicious macros are one of the most common ways malware reaches office computers. If a file asks you to enable content to "see the document", close it.

Digital signatures

A developer can sign a project (in VBE choose Tools > Digital Signature). Colleagues who trust the certificate can run the macro under the strict setting. For personal tools you can create a self-signed certificate with the Selfcert utility that ships with Office.

Stand-in: an editor-like Immediate window

The Immediate window evaluates expressions. Here is a JavaScript stand-in (not VBA) that mimics it, to show why trying one line at a time is so useful.

JS Stand-in: Immediate window

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In real VBA the same lines are ? 2 + 3, ? UCase("vba"), ? Len("macro") and ? 10 Mod 3, giving 5, VBA, 5 and 1.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut opens the Visual Basic Editor from Excel?

    Alt+F11 toggles between the application and the VBE. Alt+F4 closes the window.
  2. 2. Where do you type one-off statements to test them instantly?

    The Immediate window (Ctrl+G) executes a single line when you press Enter.
  3. 3. Which macro setting is the safest sensible default?

    You decide per file after the yellow security bar appears. Enabling everything is unsafe.
  4. 4. What is a Trusted Location?

    Choose private, controlled folders only, never Downloads.

Summary

  • Enable the Developer tab, open the editor with Alt+F11.
  • Key windows: Project Explorer, Code, Properties, Immediate, Locals, Watch.
  • Tick Require Variable Declaration in Tools > Options.
  • Keep macro security at "disable with notification" and only trust files you know.

Next: Your First Macro