Yes, you can code inside Excel. The built-in way is VBA (Visual Basic for Applications), a programming language included with the desktop versions of Excel for Windows and Mac. Coding in Excel lets you automate repetitive tasks, build custom functions, clean up data in one click, and create small tools inside your workbooks. The quickest way in is to enable the Developer tab, open the VBA editor, write a short macro, and run it. This guide walks through that from scratch.
What You'll Need
- Excel for desktop (Microsoft 365, Excel 2019 or later is easiest). Note that the web version of Excel cannot create or run VBA macros.
- A workbook saved as a macro-enabled file (.xlsm) if you want to keep your code.
- Basic familiarity with Excel itself, such as selecting cells and writing simple formulas.
- Optional but helpful: a sample workbook with dummy data, so mistakes never touch anything important.
Step-by-Step
- Enable the Developer tab. On Windows, right-click anywhere on the ribbon and choose "Customize the Ribbon", then tick "Developer" in the list on the right and click OK. On Mac, open Excel's preferences from the Excel menu and look for the ribbon customization option to turn on the Developer tab. Exact wording can differ slightly between versions and platforms.
- Save as a macro-enabled workbook. Go to File > Save As and pick "Excel Macro-Enabled Workbook (*.xlsm)" as the file type. A normal .xlsx file will not keep your code.
- Open the VBA editor. Press Alt+F11 on Windows (or Fn+Option+F11 on many Macs), or click the Visual Basic button on the Developer tab. This opens a separate editor window where all Excel code lives.
- Insert a module. In the editor, right-click your workbook name in the Project pane on the left, choose Insert > Module. A module is simply a blank code file inside your workbook.
- Write your first macro. In the module, type:
Sub HelloExcel()
MsgBox "Hello from Excel!"
End Sub
Every macro starts withSub, ends withEnd Sub, and runs the lines in between. - Run it. Click anywhere inside the code and press F5. A message box should pop up. Congratulations, you have coded in Excel.
- Write a macro that touches cells. Try this one to see how code controls your sheet:
Sub FillColumnA()
Dim i As Integer
For i = 1 To 10
Cells(i, 1).Value = i * 2
Next i
End Sub
Run it and column A fills with the numbers 2 to 20.Cells(row, column)is how you point at any cell from code. - Test and debug. Press F8 instead of F5 to run the code one line at a time so you can watch what each line does. If a line errors out, read the message, click Debug, and Excel highlights the exact line that failed.
- Save and reuse. Save the .xlsm file. To run a macro later without opening the editor, press Alt+F8, select the macro name, and click Run. You can also add a button: on the Developer tab, use Insert to add a Form Control button and assign your macro to it.
- Record a macro to learn faster. On the Developer tab, click Record Macro, do some actions in the sheet (format cells, insert a formula, sort data), then stop recording. Open the editor and read the generated code. This is one of the fastest ways to learn how VBA describes actions in Excel.
Common Mistakes
- Saving as .xlsx instead of .xlsm, which silently strips out all macros.
- Forgetting the
End Subline or mistypingSub, which causes a compile error the moment you run anything. - Hard-coding cell references that break when data moves. Prefer named ranges or code that finds the last used row.
- Not testing on a copy of the workbook. Macros cannot always be undone with Ctrl+Z, so a bad loop can overwrite real data permanently.
- Expecting macros to sync or run in Excel for the web or in the mobile apps; they only run in desktop Excel.
- Disabling all macro security permanently instead of enabling macros only for workbooks you trust.
Tips
- Start every serious macro with
Application.ScreenUpdating = Falseand turn it back on at the end; large loops run far faster. - Use
Option Explicitat the top of every module. It forces you to declare variables, which catches typos early. - Comment your code with an apostrophe (
' like this) so future-you understands it. - When you cannot figure out the code for an action, record it as a macro and read the result.
- Keep macros short and focused. Several small macros are easier to fix than one giant one.
Troubleshooting
- Macros will not run and a security warning appears: click "Enable Content" on the message bar, but only for files you created or fully trust. Check Developer > Macro Security to see the current policy.
- "Macros are disabled" with no option to enable: your organization may enforce a policy, or the file may be blocked. Right-click the file, open Properties, and tick Unblock if it came from the internet.
- Alt+F11 does nothing: try the Visual Basic button on the Developer tab instead; some keyboards need an Fn key as well.
- "Sub or Function not defined" error: you misspelled a keyword or called a macro that does not exist in that workbook. Check spelling, and press Debug to see the offending line.
- Nothing visible happens when you run a macro: you may have clicked Run on the wrong macro in the Alt+F8 list, or the code edited a different worksheet than the one you are viewing.
- Code ran but the wrong sheet changed: use explicit references like
Worksheets("Sheet1")instead of unqualifiedCells, which defaults to the active sheet.
Frequently Asked Questions
Can I code in Excel without knowing how to program?
Yes. Start with the macro recorder, which writes code for you, then read and tweak small pieces. Many useful macros are a handful of lines.
Is VBA the only way to code in Excel?
No. Office Scripts (a TypeScript-based option available with Microsoft 365 online) and Office Add-ins built with JavaScript are alternatives, but VBA is the classic, built-in choice for desktop Excel.
Does Excel for the web support macros?
Not VBA macros. Excel for the web cannot create or run VBA; for browser-based automation, Microsoft 365 offers Office Scripts instead.
Are macros safe?
Macros you write yourself are safe. The risk comes from enabling macros in files from unknown sources, so enable content only for trusted files.
Can a macro be undone with Ctrl+Z?
Usually not. Most macro actions cannot be undone, so work on a copy of important data or have the macro make a backup first.
Where is my code stored?
Inside the workbook itself, in the .xlsm file. Emailing or moving the file carries the code with it, and saving as .xlsx deletes it.