VBA (Visual Basic for Applications) lets you automate repetitive tasks in Microsoft Office programs like Excel, Word, and Outlook. The basic code examples below show you how to open the VBA editor, write a simple macro, run it, and save your work so the code is not lost. No programming experience is needed—just follow the steps in order.

What You'll Need

  • A desktop version of Microsoft Office (Excel, Word, or Outlook) installed on Windows or Mac. The steps below use Excel, but the VBA editor works the same way in other Office apps.
  • Macro security settings that allow macros to run. By default, Office disables macros from the internet, so you may need to enable them for your own files.
  • A blank workbook or an existing file where you can safely test code. Avoid running new macros on important files until you are comfortable.

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt+F11 (Windows) or Fn+Option+F11 (Mac). You can also go to the Developer tab and click Visual Basic. If you do not see the Developer tab, right-click the ribbon and choose Customize the Ribbon, then check Developer in the list on the right. Menu labels can differ slightly by version, device, or region.
  2. Insert a module. In the VBA editor, go to Insert > Module. A blank code window appears. Modules are where you store macros that you want to run manually or assign to buttons.
  3. Write a simple message box macro. Type the following code exactly as shown:
    Sub HelloWorld()
        MsgBox "Hello, world!"
    End Sub
    This macro displays a pop-up message. The word Sub starts the macro, and End Sub ends it. Everything between runs when you execute the macro.
  4. Run the macro. Click anywhere inside the code and press F5, or go to Run > Run Sub/UserForm. A message box should appear. Click OK to close it. If nothing happens, make sure your cursor is inside the Sub and End Sub lines.
  5. Write a macro that writes to a cell. Replace the previous code with this example:
    Sub WriteToCell()
        Range("A1").Value = "Hello from VBA"
    End Sub
    Run it with F5. Switch back to Excel (Alt+F11 again) and check cell A1 on the active sheet. You should see the text. This shows how VBA can read and write worksheet data.
  6. Write a macro that loops through cells. Clear the code window and paste this:
    Sub FillNumbers()
        Dim i As Integer
        For i = 1 To 10
            Cells(i, 1).Value = i
        Next i
    End Sub
    Run it. Column A rows 1 through 10 fill with the numbers 1 to 10. The Dim line declares a variable, and the For...Next loop repeats the action. This pattern is the basis for many automation tasks.
  7. Save your workbook as a macro-enabled file. Go back to Excel and use File > Save As. Choose Excel Macro-Enabled Workbook (*.xlsm) from the file type list. If you save as a regular .xlsx file, Excel removes the macros without warning. On Mac, the file type list may appear in a dropdown; choose the macro-enabled option.
  8. Assign a macro to a button (optional). On the Developer tab, click Insert > Button (Form Control), then draw a button on the sheet. A dialog asks which macro to assign. Pick one of your macros and click OK. Now you can run it with a single click. If you do not see the Developer tab, follow the ribbon customization step from earlier.
  9. Test and adjust. Run each macro on a copy of your file first. If a macro errors, press the Debug button in the error dialog to see which line is highlighted. Common fixes include checking quotation marks, ensuring cell references are valid, and confirming the active sheet is the one you expect.

Common Mistakes

  • Saving as .xlsx. Macros are stripped from standard workbooks. Always save as .xlsm if you want to keep your code.
  • Forgetting to enable macros. If the code does not run, check the yellow security bar or the Trust Center settings. Macros from unknown sources stay disabled by default.
  • Typing code outside a Sub. Statements must sit between Sub and End Sub. Code placed above or below will not run and may cause a compile error.
  • Using the wrong sheet reference. Cells(i, 1) refers to the active sheet. If you switch sheets, the macro writes to a different place. Use Worksheets("Sheet1").Cells(i, 1) to be explicit.
  • Missing quotation marks. Text values must be wrapped in double quotes. Numbers and cell references do not need quotes.

Tips

  • Use the macro recorder to capture actions, then open the recorded code to learn the syntax. Go to Developer > Record Macro, perform a few steps, then stop recording and press Alt+F11 to inspect the generated code.
  • Press F8 to step through code one line at a time. This helps you see exactly where a problem occurs.
  • Add comments with a single quote (') to explain what each block does. Comments are ignored when the macro runs.
  • Keep macros short and focused. A macro that does one thing is easier to debug than one that tries to do everything.
  • Back up your workbook before running unfamiliar code. VBA can delete data or change files, and there is no undo for most macro actions.

Troubleshooting

  • The macro does not appear in the list. Make sure the code is inside a module, not a sheet or ThisWorkbook object. Also check that the Sub name is spelled correctly and has no spaces.
  • A security warning blocks the macro. Click Enable Content if you trust the file. For files you created, you can add the folder to Trusted Locations in Trust Center settings. Do not enable macros from sources you do not trust.
  • Compile error: Expected: end of statement. This usually means a missing parenthesis, quote, or line break. Check the highlighted line and the line above it.
  • Run-time error '1004'. This often means the cell reference is invalid or the sheet is protected. Unprotect the sheet or adjust the range, then try again.
  • Nothing happens when you press F5. Click inside the Sub you want to run. If the cursor is outside any Sub, F5 may run a different macro or show a dialog asking which one to run.
  • Macros are disabled by policy. In some organizations, macro execution is blocked by IT policy. In that case, contact your administrator; you cannot override it locally.

Frequently Asked Questions

Do I need to know programming to write VBA?

No. Basic macros can be created by copying short examples and changing cell references or text. As you practice, you will learn the patterns.

Can I undo a macro?

Most macro actions cannot be undone with Ctrl+Z. Save a backup copy before running a new macro so you can restore your data if needed.

Will my macros work on Mac?

Most basic VBA code works on both Windows and Mac, but some features (such as certain file dialogs or Windows-only APIs) may behave differently. Test on your platform.

How do I share a workbook with macros?

Save as .xlsm and send the file. Recipients will see a security warning and must enable macros to run the code. Never enable macros from people you do not trust.

Can I record a macro instead of writing code?

Yes. The macro recorder captures your actions and generates VBA code. It is a good way to learn syntax, but recorded code can be verbose and may include unnecessary steps.

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

Code in a standard module is available to all sheets and can be run manually. Code in a sheet module is tied to events on that sheet, such as activating the sheet or changing a cell.