Excel 365 VBA (Visual Basic for Applications) lets you record or write small macros that automate repetitive spreadsheet work - formatting, filtering, copying data between sheets, saving files, sending emails, and so on. The shortest working set of steps is: open the Developer tab (File > Options > Customize Ribbon, tick Developer), click Visual Basic or Record Macro, write or record the code, then run it with Alt+F8 or F5. Everything below builds on that, with real example patterns you can paste and adapt. Note that ribbon and menu labels can differ slightly by Excel version, device and region, so the exact wording you see may vary.

What You'll Need

  • Excel 365 for Windows or Mac with macros enabled. VBA is not available in Excel for the web or the mobile apps - you need the desktop application.
  • The Developer tab visible in the ribbon (it is hidden by default).
  • A file saved as .xlsm (Excel Macro-Enabled Workbook), not .xlsx, or your code will be lost when you close the file.
  • Basic familiarity with cells, ranges and sheets - you do not need programming experience, but you should know what a cell reference like A1 or a sheet name is.
  • On some managed or corporate machines, macro execution is blocked by policy. If so, talk to your IT administrator rather than trying to circumvent it.

Step-by-Step

  1. Turn on the Developer tab. Go to File > Options > Customize Ribbon, and in the right-hand list tick the Developer checkbox, then click OK. On Mac, open Excel > Preferences and look for the Ribbon & Toolbar settings, or use the Developer menu in the menu bar. Once visible, the Developer tab gives you the Visual Basic editor, Macros dialog and Record Macro button.
  2. Save as a macro-enabled workbook first. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Do this before writing anything, so you never lose code to a format prompt later.
  3. Record a macro to see the pattern. On the Developer tab click Record Macro, give it a name such as FormatReport, and perform the action you want to automate - for example bold the header row, apply a number format, and set a column width. Click Stop Recording. Now click Macros (or press Alt+F8), select your macro and click Edit to see exactly which objects and properties Excel used. This is the fastest way to learn the vocabulary: you are shown real code for real actions rather than guessing at method names.
  4. Write your first clean macro. In the Visual Basic editor, use Insert > Module to add a blank module, then type a small procedure. A standard pattern is:
    Sub BoldHeaders()
      With ActiveSheet.Range("A1:Z1")
        .Font.Bold = True
        .Interior.Color = RGB(220, 220, 220)
      End With
    End Sub
    Press F5 to run it, or Alt+F8 from the worksheet. Modules are where general-purpose code lives; they are visible to the whole workbook and are the right home for utility macros.
  5. Reference sheets explicitly instead of ActiveSheet. Recorded code almost always says ActiveSheet, which fails as soon as the user is on a different tab. Replace it with ThisWorkbook.Worksheets("Report") so the macro always touches the right sheet regardless of what is selected.
  6. Loop through rows to process data. A typical example walks down a column and changes each cell:
    Dim lastRow As Long, i As Long
    lastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To lastRow
      If Cells(i, 3).Value > 1000 Then Cells(i, 4).Value = "Review"
    Next i
    Finding the last row with End(xlUp) is the standard technique because it adapts to however much data you have, unlike a hard-coded range.
  7. Copy data between sheets or workbooks. Use an explicit range transfer such as Worksheets("Data").Range("A1:D10").Copy Destination:=Worksheets("Summary").Range("A2"). If you only want values rather than formatting, set the destination range's .Value equal to the source range's .Value instead of using Copy.
  8. Add user interaction with InputBox and MsgBox. Dim ans As String then ans = InputBox("Which month?") collects a value, and MsgBox "Done: " & ans reports back. This turns a rigid macro into a small reusable tool without building a full form.
  9. Speed things up. Wrap heavy loops with Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual at the start, then restore them to True and xlCalculationAutomatic at the end - including in an error handler, so a crash does not leave Excel in a frozen-looking state.
  10. Handle errors gracefully. Add On Error GoTo CleanFail near the top of the procedure, put the restore lines under a CleanFail: label, and use Resume Next or a message box to tell the user what went wrong. Unhandled errors drop the user into the VBA editor with a cryptic message.
  11. Attach a macro to a button. On the Developer tab choose Insert > Button (Form Control), draw it on the sheet, and pick your macro in the dialog. Right-click the button and choose Edit Text to label it. This is how you hand a macro to colleagues who will never open the editor.
  12. Test on a copy, then save. Run the macro against a duplicate of your file, confirm the results, and keep a backup of the working version before you distribute it.

Common Mistakes

  • Saving as .xlsx. Excel warns that macro-free formats cannot store VBA. If you click through the prompt, the code is stripped out. Always keep the .xlsm extension.
  • Relying on ActiveSheet, ActiveCell or Selection. These depend on where the user is clicking. Name your sheets and ranges explicitly.
  • Hard-coding row counts. Ranges like A1:A1000 break the moment your data grows. Calculate the last row instead.
  • Forgetting to reset ScreenUpdating. If the macro errors out before the restore line, Excel can look unresponsive. Put the restore in the error handler.
  • Using Select and Activate everywhere. Recorded macros are full of them; they are slow and fragile. Refer to objects directly instead.
  • Assuming macros work everywhere. VBA does not run in Excel for the web or the mobile apps, so a shared file can look broken on those platforms.

Tips

  • Use the macro recorder as a dictionary, not a finished product - record, read, then rewrite the result cleanly.
  • Give procedures descriptive names and keep each one small and single-purpose; you can call them from one master macro.
  • Comment generously with an apostrophe at the start of the line, especially on anything involving business rules.
  • Prefer Worksheets("Name") over sheet index numbers, which change when tabs are moved.
  • Keep a personal macro workbook (Excel offers this when recording) for utilities you want available in every file.
  • Store configuration values, such as thresholds or sheet names, in named cells or constants rather than buried in code.

Troubleshooting

If the Developer tab is missing, it has simply not been enabled - repeat step 1. If macros are greyed out or you see a security warning bar, check File > Options > Trust Center > Trust Center Settings for the macro settings; in managed environments the policy may be locked. If a macro runs but does nothing, confirm the sheet name in your code matches the tab exactly, including spaces, and that the workbook you edited is the one currently active. If you get a "Subscript out of range" error, that usually means a sheet or workbook name in the code does not exist. If the file opens with macros disabled, you may need to click Enable Content on the message bar - only do this for files you trust.

If that did not work, step through the code line by line: click inside the procedure and press F8 repeatedly to execute one statement at a time and see exactly where it fails. Also try Debug > Compile VBAProject to catch syntax errors before running.

Frequently Asked Questions

Does VBA work in Excel for the web?

No. VBA requires the desktop version of Excel on Windows or Mac. Files with macros open in the browser but the code will not run.

What is the difference between .xlsm and .xlsb?

Both store macros. .xlsm is the standard macro-enabled workbook; .xlsb is a binary format that can open and save faster with very large files. For most purposes .xlsm is the safer, more portable choice.

Can I undo a macro's changes?

No. Excel's undo stack does not cover VBA actions. Always test on a copy, and consider having the macro save a backup before it makes bulk changes.

How do I share a macro with a colleague?

Send the .xlsm file itself, and tell them to click Enable Content on the security bar. Do not send .xlsx copies, which strip out the code.

Can VBA send emails or open other files?

Yes, VBA can automate mail in Outlook and open, save or close other workbooks. These actions are powerful and can be disruptive if looped, so test them carefully and add confirmation prompts.