How to Use VBA Macro in Excel: Step-by-Step

VBA & macros · Updated November 23, 2026

VBA (Visual Basic for Applications) macros let you automate repetitive tasks in Excel—like formatting, data cleaning, or report generation—with a single click. This guide walks you through enabling the Developer tab, recording a simple macro, writing your own code, and running it safely. Before you start, save a copy of your workbook: macros cannot be undone with Ctrl+Z, so always test on a backup file.

What You’ll Need

  • Microsoft Excel for Microsoft 365, Excel 2021, or Excel 2019 (the steps are similar for earlier versions).
  • A workbook you’re willing to experiment on—create a sample file with a few rows of data.
  • Basic familiarity with Excel’s interface (ribbon, sheets, cells). No programming experience is required for recording, but writing code helps.
  • If your Excel version is older than 2016, the steps are the same, but some menu names may differ slightly.

Step-by-Step

  1. Enable the Developer tab. In Excel, click File > Options > Customize Ribbon. Under Main Tabs, check the Developer box and click OK. On a Mac, go to Excel > Preferences > Ribbon & Toolbar, then check Developer.
  2. Save your workbook as a macro-enabled file. Before recording or writing code, press Ctrl+S (or Cmd+S on Mac). In the Save As dialog, choose Excel Macro-Enabled Workbook (*.xlsm) from the Save as type dropdown. This is required for macros to be stored.
  3. Record a simple macro (the easiest way to start). Go to the Developer tab and click Record Macro. Give it a name (no spaces, start with a letter), assign a shortcut key if you like (e.g., Ctrl+Shift+R), and choose where to store it. Select This Workbook to keep it in the current file. Click OK.
  4. Perform the actions you want to automate. For example, select a range of cells, apply bold formatting, change the font color, or autofit columns. Every click and keystroke is being recorded.
  5. Stop recording. Click Stop Recording on the Developer tab. Your macro is now saved and can be run anytime.
  6. Run the recorded macro. Press Alt+F8 (or Option+F8 on Mac) to open the Macro dialog. Select your macro and click Run. Alternatively, use the shortcut you assigned.
  7. Write your own VBA code (for more control). In the Developer tab, click Visual Basic (or press Alt+F11). This opens the VBA editor. In the menu, click Insert > Module to add a new code module.
  8. Type a simple macro. In the module window, paste this example that formats the active cell bold and red:
    Sub FormatActiveCell()
        ' Make the active cell bold and red
        ActiveCell.Font.Bold = True
        ActiveCell.Font.Color = RGB(255, 0, 0)
    End Sub

    Close the VBA editor (click the X) to return to Excel.

  9. Run your custom macro. Select a cell, press Alt+F8, choose FormatActiveCell, and click Run. The cell should become bold and red.
  10. Assign a macro to a button (optional). For one-click access, insert a shape (Insert > Shapes) or a button (Developer > Insert > Form Controls > Button). Right-click the button, choose Assign Macro, select your macro, and click OK.
  11. Save your work. Press Ctrl+S to save the file as .xlsm. If you close without saving, the macro is lost.

Common Mistakes

  • Forgetting to save as .xlsm. If you save as .xlsx, Excel strips the macro code, and you’ll lose it.
  • Using spaces or special characters in macro names. Macro names must start with a letter and cannot contain spaces. Use underscores (e.g., My_Macro).
  • Recording actions that depend on the selected cell. If you record while a specific cell is selected, the macro will always start from that cell. Use relative references (Developer > Use Relative References) to record actions relative to the current selection.
  • Not enabling macros when opening a file. When you open a .xlsm file, a yellow bar may appear. Click Enable Content to allow macros to run. If you don’t, the macro won’t work.
  • Writing code with syntax errors. A missing parenthesis or a typo can cause a compile error. Always test your code with a small sample.

Tips

  • Use the macro recorder to learn VBA. Record a simple task, then open the VBA editor and read the generated code. It’s a great way to see real syntax.
  • Comment your code. Start each line with an apostrophe (') to add notes. This helps you remember what each part does.
  • Use variables for flexibility. For example, instead of hardcoding a range, use a variable to refer to the selected range.
  • Test on a copy. Always run macros on a duplicate file first, especially if they delete data or modify many cells.
  • Keep macros short. If a macro becomes long, break it into smaller subroutines that call each other.
  • Use Application.ScreenUpdating = False at the start of long macros to speed them up, and set it back to True at the end.

Troubleshooting

  • Macro isn’t running. Check that the file is saved as .xlsm and that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose Disable VBA macros with notification (or Enable all macros temporarily). On a Mac, check Excel > Preferences &em; Security.
  • “Compile error: User-defined type not defined.” This often means you’re missing a reference. In the VBA editor, go to Tools > References and ensure the needed libraries (like “Visual Basic for Applications”) are checked.
  • Macro works on one sheet but not another. Your code might reference a specific sheet name. Use ActiveSheet or ThisWorkbook.Sheets("Sheet1") carefully.
  • Recorded macro does nothing. If you recorded without selecting any cells, the macro may be empty. Delete it and re-record, making sure to perform at least one action.
  • “Cannot run the macro” error. This can happen if the macro is stored in a different workbook. In the Macro dialog, check the Macros in dropdown to select the correct workbook.
  • Undo doesn’t work after running a macro. That’s normal—macros can’t be undone. That’s why saving a backup is essential.

Frequently Asked Questions

  • Can I use VBA macros in Excel for the web? No, Excel for the web does not support VBA. Macros only run in the desktop versions for Windows and Mac.
  • Is VBA the same as a macro? A macro is a set of instructions; VBA is the programming language used to write those instructions. The macro recorder generates VBA code for you.
  • How do I delete a macro? Press Alt+F8, select the macro, and click Delete. For code in modules, open the VBA editor, right-click the module in the Project Explorer, and choose Remove Module.
  • Can I run a macro automatically when a workbook opens? Yes, by using the Workbook_Open event. In the VBA editor, double-click ThisWorkbook in the Project Explorer and paste code like Private Sub Workbook_Open() with your macro call.
  • Why is my macro not working on another computer? Macro settings might be stricter, or the file might be blocked. Ensure macros are enabled and the file is unblocked (right-click file > Properties > Unblock).
  • Can I share macros with others? Yes, by sharing the .xlsm file. However, be cautious: macros can contain harmful code. Only run macros from trusted sources.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.