Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA in Excel 365: Build Your First Macro

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

VBA (Visual Basic for Applications) is the built-in programming language in Excel 365 that lets you automate repetitive work — formatting reports, combining sheets, cleaning imported data, or building a simple form for data entry. To get started, open the Developer tab, record or write a macro, and save the workbook as an .xlsm file. That is the shortest working path; everything after that is practice. Note that Excel 365 for the web cannot run VBA, so you need the desktop app on Windows or Mac. Menu labels can differ slightly by version and platform, so treat the exact wording below as a guide.

What You'll Need

  • Excel 365 desktop app installed on Windows or Mac (the browser version does not support VBA).
  • Permission to change Excel settings on your machine — the Developer tab is hidden by default.
  • A macro-enabled workbook saved as .xlsm. Ordinary .xlsx files cannot keep VBA code.
  • A trusted workbook or a file you created yourself. Do not enable macros in files from unknown senders.

Step-by-Step

  1. Turn on the Developer tab. On Windows, go to File > Options > Customize Ribbon, tick Developer in the right-hand list, and click OK. On Mac, open Excel > Preferences > Ribbon & Toolbar and tick Developer. The tab then appears on the ribbon.
  2. Check the macro security setting. Open File > Options > Trust Center > Trust Center Settings > Macro Settings. The default is usually "Disable all macros with notification," which shows a yellow security bar when a workbook contains macros. Leave this on; do not set it to "Enable all macros," because that removes your main protection against malicious code.
  3. Save the file as macro-enabled first. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Doing this before you write code saves you from losing work later, since .xlsx files discard VBA on save.
  4. Open the editor. Click Developer > Visual Basic (or press Alt+F11 on Windows; on Mac use the Developer tab). A separate window opens with a Project pane on the left, showing your open workbooks.
  5. Insert a module. In that window, right-click your workbook name in the Project pane and choose Insert > Module. A blank code window appears. Modules hold general-purpose macros; code in a sheet object only runs on that sheet's events.
  6. Write your first macro. Paste this into the module:
    Sub HelloMacro()
        MsgBox "Excel VBA is working"
    End Sub
    Press F5 to run it. A message box should appear. If it does, your environment is ready.
  7. Try a practical macro. Replace the code with something useful, such as bolding the first row of the active sheet:
    Sub FormatHeader()
        Rows(1).Font.Bold = True
        Rows(1).Interior.Color = RGB(221, 235, 247)
    End Sub
    Run it with F5. Change Rows(1) to Rows(3) and run again to see how the target changes.
  8. Record a macro if you are unsure of the syntax. Click Developer > Record Macro, give it a name with no spaces (such as CleanReport), perform the actions you want once, then click Developer > Stop Recording. Open the Visual Basic editor to see the code Excel generated. Recorded code is often verbose, but it is a reliable way to discover the exact object and property names.
  9. Connect a macro to a button. Go to Developer > Insert > Button (Form Control), draw the button on the sheet, and pick your macro in the dialog that appears. You can then rename the button text by right-clicking it.
  10. Save and close properly. Save with Ctrl+S. Because the file is .xlsm, Excel may ask whether to keep the macro format — choose to keep it. When you reopen the file, click "Enable Content" on the security bar only if you created or trust the workbook.

Common Mistakes

  • Saving as .xlsx. The code vanishes without a clear warning. Always confirm the format in the title bar or via Save As.
  • Running the wrong procedure. Pressing F5 in the editor runs whichever macro the cursor is inside, not necessarily the one you want. Use Run > Run Sub/UserForm or pick the macro from the Macros dialog.
  • Recorded code that references a specific range. The recorder usually writes something like Range("B2").Select, which only works on that sheet and layout. Edit it to use named ranges or ActiveCell so the macro works on other data.
  • Disabling macro security to "fix" things. This is a real security risk. Instead, add your own workbooks folder to Trust Center's trusted locations, which is the intended way to avoid prompts for files you control.
  • Forgetting Option Explicit. Without it, a typo like Rangez silently becomes a new variable instead of an error, and you spend an hour debugging nothing. Add it at the top of the module.

Tips

  • Press F8 to step through code one line at a time, which makes it far easier to see exactly where something goes wrong.
  • Use the Immediate window (View > Immediate Window, or Ctrl+G in the editor) to test single lines, for example ?ActiveSheet.Name, without running the whole macro.
  • Name macros without spaces or punctuation; Excel rejects names like Clean Report.
  • Keep a personal workbook or a dedicated .xlsm "toolbox" for macros you reuse across files, rather than pasting code into every new workbook.
  • Comment your code with an apostrophe, for example 'copies the summary sheet, so a macro you wrote six months ago still makes sense.

Troubleshooting

The Developer tab is missing. Recheck the Customize Ribbon list; it is easy to tick the wrong checkbox level. If your organisation manages Excel settings centrally, the tab may be locked and you will need to ask IT.

A macro runs but nothing happens. The code may be acting on a different sheet or range than you expect. Run it step by step with F8 and check the sheet name shown in the Project pane.

"Cannot run the macro" or a compile error appears. Look for a red-highlighted line in the editor — that is where the syntax error is. A missing End Sub, an unclosed quote, or a misspelled object name are the usual culprits.

You get a security warning every time you open the file. Confirm the file is genuinely .xlsm and saved in a folder you trust, and consider adding that folder to Trust Center's trusted locations rather than lowering the overall security level.

Macros seem disabled entirely. Some managed work environments block VBA by policy. If the Macro Settings options are greyed out, that is an organisational setting, not something to work around.

The file will not open at all. Open Excel first and use File > Open to pick the file; double-clicking an .xlsm with a broken macro can hang. If it still fails, restore a previous version from your backup rather than trying to repair the code blindly.

Frequently Asked Questions

Does VBA work in Excel for the web?

No. The browser version of Excel does not run VBA macros. You need the desktop app on Windows or Mac to write and run them.

Do I need to know programming already?

No. Starting with the macro recorder and editing small pieces of the generated code is the usual way people learn. The Immediate window and F8 stepping help you check each change.

Why did my macros disappear after saving?

Almost always the file was saved as .xlsx, which strips VBA. Re-save as .xlsm and rebuild that session's code.

Is it safe to enable macros in a downloaded workbook?

Only if you trust the source and have verified it. Macro security exists precisely because VBA can run harmful code. Keep the default "Disable all macros with notification" setting and enable content only for files you know.

Can VBA code be password protected?

Yes, the editor lets you protect a project with a password, but that protection is weak and should not be treated as real security. Use it to stop casual edits, not to hide sensitive information.

What is the difference between a module and a sheet in the editor?

A standard module holds macros you run manually or from buttons. Code attached to a sheet object runs in response to events on that sheet, such as a cell changing, and should be kept separate.