LearnExcel.io
Menu

How to Write a Macro in Excel

Written by ··Updated June 16, 2026
How to Write a Macro in Excel

To write a macro in Excel, turn on the Developer tab, then either record your actions with Record Macro or open the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on Mac) and type your own code between Sub and End Sub. Save the file as a macro-enabled workbook (.xlsm) so your code is kept, and enable macros when you reopen it.

If you are looking to automate repetitive tasks in Excel, you may want to consider writing macros. A macro is a set of instructions that automate tasks in Excel, and it can save significant time and effort in your work. Macros are also helpful when you need to perform the same task multiple times. Writing a macro in Excel may sound intimidating, but it is well within reach with a bit of guidance. In this blog post, we will cover the essentials of how to write a macro in Excel.

What is a Macro in Excel?

A macro is a set of instructions created to automate tasks in Excel. It can execute a series of commands with just one click or shortcut key. Macros are useful when you need to perform the same task repeatedly because their efficiency saves significant time.

How to Write a Macro in Excel

Step 1: Enable the Developer Tab

Before you start to write a macro in Excel, you need to enable the Developer tab in the ribbon. Go to File > Options > Customize Ribbon. Check the Developer box, and click OK. On a Mac, choose Excel > Preferences > Ribbon & Toolbar instead, then tick Developer and click Save. If you get stuck, see our guide on how to add the Developer tab in Excel or enable the Developer tab.

Step 2: Record the Macro

Once you have the Developer tab enabled, select the Record Macro button in the Code group. This will open up the Record Macro dialog box. In the Macro name field, type the name you want for your macro. You can also assign a shortcut key to activate your macro. Select the location where you want to store your macro, and click OK.

Step 3: Perform the Actions

Now that you’ve started recording your macro, the actions you perform in Excel will be recorded. Make sure you perform all the necessary steps for your macro to work correctly. Remember that every action counts, so keep it precise and streamline your workflow.

Step 4: Stop the Macro

Once you’ve completed all the actions you need to record, you can stop recording the macro by selecting the Stop Recording button in the Code group. It’s that simple.

Step 5: Use the Macro

Now it’s time to use your macro. Whenever you need to repeat the same process, activate the Developer tab, and click the Macros button. Choose the name of your macro, and then click Run. This will execute your macro and perform the recorded actions. For more detail on running and managing saved macros, see how to run a macro in Excel.

Recording vs. Writing a Macro by Hand

The steps above use the macro recorder, which is the fastest way to get started because Excel writes the VBA code for you while you click. Recording is ideal for simple, repeatable actions, but it can produce bulky code and it cannot capture logic such as loops, conditions, or pop-up prompts. When you need that flexibility, write the macro by hand in the Visual Basic Editor. Many people do both: record a macro first, then open it in the editor and clean up or extend the generated code.

Writing a Macro in the VBA Editor

To write your own macro from scratch, open the Visual Basic for Applications (VBA) editor with Alt+F11 on Windows or Option+F11 (Fn+Option+F11 on some keyboards) on Mac. You can also open it from the Developer tab by clicking Visual Basic. For a walkthrough, see how to open the VBA editor in Excel.

In the editor, go to Insert > Module to create a blank module, then type your macro between a Sub and End Sub block. Every macro is a procedure that starts with Sub, the macro name, and a pair of parentheses, and ends with End Sub:

Sub GreetUser()
    MsgBox "Hello! Your macro is working."
End Sub

Press F5 (or Run > Run Sub/UserForm) to execute it. To add repetition or decisions, you can use a loop in Excel VBA. Once you are comfortable, you can edit existing macros directly in the editor. If you want a deeper foundation, our beginner’s guide to Excel macros and VBA covers the language in more detail.

Mac vs. Windows: VBA Differences

VBA works on both Windows and Mac, but there are a few differences to be aware of:

  • The shortcut to open the editor is Option+F11 on Mac (vs. Alt+F11 on Windows), and you may need to add the Fn key depending on your keyboard.
  • The Mac VBA editor has a simpler interface and lacks some Windows-only tools, such as the macro recorder’s full feature set and certain debugging windows.
  • Some Windows-only features are unavailable on Mac, including ActiveX controls, certain Windows API calls, and UserForm controls like the date picker.
  • File and folder paths use a different separator, so code that builds paths may need adjusting between platforms.

For everyday recording and simple Sub routines, however, the process is essentially the same on both platforms.

Saving a Macro-Enabled Workbook (.xlsm)

A standard .xlsx workbook cannot store macros. To keep your code, save the file as a macro-enabled workbook: choose File > Save As, then pick Excel Macro-Enabled Workbook (*.xlsm) from the file-type list. If you save as plain .xlsx, Excel warns you and strips out the macros.

When you reopen an .xlsm file, Excel usually disables macros for safety and shows a security warning. Click Enable Content (Windows) or allow macros in the dialog to run them. See how to enable macros in Excel for the full steps. If a file is blocked with a “Microsoft has blocked macros from running” message, you may need to unblock the macros. When you want to turn the feature off, you can disable macros instead.

Final Thoughts

Macros can be a powerful tool to increase your productivity and efficiency in Excel. With these simple steps, you can start creating your macros, automate repetitive tasks, and save significant time. Remember to streamline your workflow when recording actions, and keep your macros organized. Have fun creating your macros, and feel free to experiment with different actions.

Best Practices for Writing Macros in Excel

When creating macros in Excel, it’s important to follow best practices to ensure they work efficiently and are easy to maintain. Here are some tips:

Keep it Simple and Streamlined

When recording your macros, keep it simple. Avoid recording actions that are specific to one workbook or worksheet. Aim for broad actions that can be applied to any workbook. This way, you reduce the risk of errors and make it easier to maintain your macros.

Avoid Using Absolute References

Avoid using absolute references (e.g., A1, B1:C10) when recording macros, as they can limit the functionality of the macro. Instead, use relative references (e.g., the cell to the right, the cell in the row below) to increase versatility.

Use Comments

Adding comments to your macros can help you and others easily understand its functionality. Use comments to explain what the macro is doing, what the input and output are, and any other relevant information.

Test and Debug Your Macro

Before using your macro frequently, make sure to test it and debug any issues. This will help you avoid unnecessary mistakes and make sure that your macros perform the actions as intended.

Regularly Update Your Macros

As you continue to use your macros, updates may be necessary to match changes in the workflow or the program’s version. Keep your macros up-to-date to ensure that they work efficiently and accurately.

If you frequently perform repetitive tasks in Excel, writing a macro can save you significant time and effort. With this step-by-step guide and best practices, you can start creating macros in Excel and optimizing your workflow. Remember to keep your macros simple, test and debug, and keep them up-to-date. Happy macro writing!

Frequently Asked Questions

Here are answers to some common questions about writing macros in Excel:

What are the benefits of using macros?

Macros can save a considerable amount of time by automating repetitive tasks in Excel. Also, they can reduce the possibility of human errors in manual operations.

Do I have to be a programmer to write a macro?

Not necessarily. Although basic programming knowledge can help to make the process easier, you can still create a workable macro with no coding skills by recording actions.

What are absolute and relative references in macros?

Absolute references in macros mean that the macro runs the same way regardless of where it’s placed on the sheet, which may limit its functionality. In contrast, relative references allow the macro to record actions based on its position on the spreadsheet, making it versatile.

How can I fix a macro if it’s not working correctly?

If you got an error message when running the macro, review the task and see if the set of instructions behind the macro is logical. Check the spelling in the codes, and make sure that the cell range exist on your sheet. You can also test the macro on a new excel sheet to pinpoint the issue and debug the code accordingly.

Can a macro harm my computer?

As with any software, it’s essential to take necessary precautions when running macros in Excel. Beware of downloading and running untrusted macros, as this can infect your computer’s data with harmful software.

What is the shortcut to open the macro editor?

Press Alt+F11 on Windows or Option+F11 on Mac to open the Visual Basic for Applications (VBA) editor. On some Mac keyboards you may also need to hold the Fn key. You can also open it from the Developer tab by clicking Visual Basic.

What file format do I need to save a macro?

Save your workbook as an Excel Macro-Enabled Workbook with the .xlsm extension. A standard .xlsx file cannot store macros and will strip your code out when saved, so always choose the .xlsm format from the Save As dialog if you want to keep your macros.

Do macros work the same on Mac and Windows?

Recording and running basic macros works the same on both platforms, and VBA itself runs on Mac and Windows. However, the Mac editor is simpler, some Windows-only features such as ActiveX controls and certain API calls are unavailable, and a few keyboard shortcuts differ, so advanced macros may need small adjustments to run cross-platform.

Related guides

View all Excel Macros and VBA guides →