How to Creat a Macro in Excel

There are two ways to “creat a macro in excel”: the quick way you copy and the durable way you understand. This page gives you both. The exact Excel answer is above; below, we build the small mental model that makes the fix stick, so the next variation of the same problem solves itself.

Exact answer

In Excel: Press Alt + F11, choose Insert ▸ Module, type Sub YourName() and press Enter — Excel adds End Sub, and everything you type between the two lines is the macro. Excel for the web cannot create, run, or edit VBA macros. Open the workbook in the Excel desktop app to work with macros.

On this page8

VBA macro: Create a Macro in Excel

Scroll code horizontally to see more
Sub GreetUser()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Range("A1").Value = "Hello from my first macro"
    ws.Range("A1").Font.Bold = True
    MsgBox "Macro finished — check cell A1.", vbInformation, "First Macro"
End Sub

The smallest complete macro worth running: a name, a worksheet reference, two actions, and a confirmation. Every macro you write later is this shape with a longer middle.

How to run this macro

  1. Press Alt + F11 to open the VBA editor.
  2. Insert > Module.
  3. Paste the code above.
  4. Press F5, or close the editor and run it from Developer > Macros.
  5. Save the file as .xlsm so the macro is kept.
▤Annotated stepsExcel
1

Press Alt + F11 to open the VBA editor.

2

Right-click your workbook in the Project Explorer and choose Insert ▸ Module — a standard module, not a sheet object.

3

Type Sub followed by a name with no spaces, then brackets, e.g. Sub GreetUser(), and press Enter; End Sub appears automatically.

4

Write the actions between the two lines, then press F5 to run and check the result.

5

Save as Excel Macro-Enabled Workbook (.xlsm) so the code survives closing the file.

Ctrl+CthenCtrl+Shift+V⌘+Cthen⌘+Ctrl+VPaste values · WindowsMac

What this does

There are two ways to get a macro into a workbook, and they suit different moments. Recording is the faster start: Developer ▸ Record Macro, name it, perform the steps in Excel, then Stop Recording, and Excel writes the VBA for you. The generated code is verbose and full of .Select and .Activate lines that mirror your mouse movements, but it is real, runnable VBA, and reading it is the quickest way to learn what an object model call looks like. Writing by hand is the second route and the one everything non-trivial ends up needing, because the recorder cannot express a loop, a condition, or a prompt. Both end in the same place: a Sub inside a standard module. The one structural rule to internalise is where code lives — a standard module (Insert ▸ Module) holds macros anyone can run, while code typed into a sheet object or ThisWorkbook is event code, scoped to that object and invisible in the Macros dialog. Before the code does anything, check the environment it needs: desktop Excel, a macro-enabled workbook (.xlsm) and macros allowed for this file. If the workbook came by email or download, Office can block its macros until the file is trusted. The difference between a quick fix and a sheet you can trust is the extra minute you spend validating “creat a macro in excel”. Start on a copy or a tiny sample, keep the affected cells visible, and compare the result with the outcome you expected before you touch the real workbook. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. The point is a workflow that saves repeating the same clicks every week, but the practical win is that someone else can open the file and understand what happened without asking you.

A worked example

Create your first macro end to end. Press Alt + F11, right-click your workbook in the Project Explorer, and choose Insert ▸ Module. In the blank window type Sub GreetUser() and press Enter — the editor supplies End Sub on its own. Fill in the body so it matches the snippet above, then press F5. Cell A1 now reads "Hello from my first macro" in bold and a dialog confirms it finished. Switch back with Alt + F11 and the macro appears under Developer ▸ Macros as GreetUser, ready to run from the sheet. Save with Ctrl + S and choose Excel Macro-Enabled Workbook (.xlsm) when the format warning appears — accepting the default .xlsx would throw the code away. Creating the first macro is mostly about learning where code is allowed to live and which save format keeps it. Once those two facts are in place, every later macro is the same three moves — module, Sub, F5 — and the only thing that changes is what goes in the middle. Step through the code once (Debug ▸ Step Into in the VBA editor) and watch the sheet after each line. It shows exactly what the macro changes before you let it run end to end.

In Google Sheets

Google Sheets does not run VBA. A macro written for Excel has to be rewritten in Google Apps Script (Extensions ▸ Apps Script), and an .xlsm file converted to Sheets keeps its cells but drops the code. Keep this page bookmarked for the next time the same question comes up. Better still, repeat the steps once in your own workbook — doing it yourself is what turns a copied answer into something you remember. The short version of “creat a macro in excel”: paste the code, keep the workbook macro-enabled, trust the file only if you know where it came from, and test on a copy. Code that refers to the active sheet acts on whichever sheet is active, so check that before you run it.

Common mistakes

  • Saving as .xlsx at the end. Excel warns once, and if you click through it the whole module is discarded — the workbook reopens with no macros and no way to recover them.
  • Putting a general-purpose macro in a sheet object rather than a module. It still runs from F5 inside the editor, which hides the problem, but it never appears in Developer ▸ Macros and cannot be assigned to a button.
  • Naming a macro with a space, a leading digit, or a reserved word. VBA rejects Sub My Macro() outright; use MyMacro or My_Macro.
  • Keeping the recorder's .Select and .Activate lines. They make the macro slow and force it to depend on which sheet happens to be in front — Range("A1").Value = 1 does directly what Select-then-ActiveCell does in two fragile steps.
  • Skipping Option Explicit at the top of the module. Without it a mistyped variable name becomes a silent empty variable instead of a compile error, and the macro fails in a way that is genuinely hard to trace.

Frequently asked questions

Should I record a macro or write one?

Record when the job is a fixed sequence of clicks and you want to see the equivalent VBA. Write by hand as soon as it needs to repeat over rows, decide between cases, or ask the user anything — the recorder cannot produce loops or conditions.

Where exactly does the code go?

In a standard module: Alt + F11, then Insert ▸ Module. Macros in a sheet object or ThisWorkbook are event code, scoped to that object, and do not show up in the Macros dialog.

How do I make one macro available to every workbook?

Record any macro with "Store macro in: Personal Macro Workbook" selected. That creates PERSONAL.XLSB, which opens hidden with Excel, and anything you put in its modules is available in every file on that machine.

Why does my new macro not appear under Developer ▸ Macros?

Either it lives in a sheet object instead of a standard module, or it is declared Private, or it takes arguments — Sub Name(x As Long) is hidden from the dialog because Excel has no way to supply x.