Excel Macros Course: Step by Step Guide
In this article
Yes, you can automate repetitive tasks in Excel using Macros. This is achieved by recording your mouse and keyboard actions and then playing them back to perform the same task instantly. To get started, you must first enable the Developer tab in your Excel ribbon, which is hidden by default because it is not used for basic data entry.
What You'll Need
- A modern version of Microsoft Excel (2010, 2013, 2016, 2019, 365, or Excel for Mac).
- A Windows PC or macOS machine capable of running Excel.
- A dataset on which you want to perform the repetitive action.
- A basic understanding of Excel functions like VLOOKUP or SUM.
Step-by-Step
- Enable the Developer Tab: Open Excel. Go to File > Options > Customize Ribbon. On the right side, check the box next to Developer and click OK. This adds a new tab to the top ribbon. On Mac, go to Excel > Preferences > Ribbon & Toolbar, select Developer from the list on the right and click OK.
- Open the Visual Basic Editor: Click the Developer tab on the ribbon. In the Code group, click Visual Basic. This opens the VBA Editor window, which is where you will write and edit your macros.
- Insert a New Module: In the VBA Editor, look at the left-hand Project Explorer pane. Right-click on VBAProject (YourWorkbookName). Hover over Insert and select Module. A blank white window will appear on the right side.
- Record Your First Macro: Return to your Excel sheet. Click the Developer tab again and click the Record Macro button. In the dialog box, give your macro a name—start with a letter, no spaces (e.g., FormatTable). You can also assign a shortcut key if you wish, though be careful not to overwrite existing shortcuts. Click OK.
- Perform the Action: Excel is now recording. Perform the exact steps you want to automate. For example, select a range of data, go to the Home tab, and apply a bold font to the headers. You will see a small record button appear in the status bar at the bottom left of the window.
- Stop Recording: Once you have finished the action, return to the Developer tab and click Stop Recording.
- View the Code: Go back to the VBA Editor window. In the left pane, double-click Module1. You will see the code that was just generated. It will look like a series of lines beginning with
Sub FormatTable()and ending withEnd Sub. This is the logic that Excel has translated from your clicks. - Test the Macro: Return to your Excel sheet. Click Macros in the Developer tab. Select FormatTable from the list and click Run. Excel will perform the recorded steps instantly.
- Assign to a Button (Optional): In the Developer tab, click Insert and select Button (Form Control) from the bottom section of the shapes menu. Draw a rectangle on your sheet. A dialog box will appear asking which macro to assign; select your macro and click OK. Now, clicking this button will run the macro.
Common Mistakes
Hardcoding Cell References: A common error in recording macros is using specific cell addresses like Range("A1"). If you move your data, the macro will break because it is looking for data in cell A1. To fix this, use Relative References. Before recording, click Use Relative Reference in the Developer tab. This ensures the macro applies the formatting to the selected cells regardless of where they are located.
Ignoring Absolute vs Relative Mode: Always check the status bar or the Developer tab to see if the Use Relative Reference button is active. If you need the macro to perform the same action on the exact same cells every time (like formatting cell A1), keep it in Absolute mode. If you want it to format the currently selected cells, switch to Relative mode.
Not Saving as Macro-Enabled: If you save your Excel file as a standard .xlsx, Excel will strip out the macro code to keep the file size small and compatible with other programs. You must save the file as a Macro-Enabled Workbook (*.xlsm) or Excel Macro-Enabled Template (*.xltm) to preserve your automation.
Tips
Plan Before You Record: Do not start recording immediately. Think through the steps you want to automate. If you realize you missed a step halfway through, you have to stop and start over. Recording a clean, logical process saves time.
Use Variables for Numbers: When writing code manually (rather than recording), use variables such as Dim count As Integer to hold values. This makes the macro flexible. Instead of writing Range("A1").Value = 100, you can write Range("A1").Value = count, allowing the macro to handle different numbers dynamically.
Keep Code Clean: Avoid recording trivial actions like clicking on empty cells or scrolling. These generate lines of code that clutter your script and can slow it down. Delete any lines of code that do not contribute to the final result.
Test on a Copy: Always open a copy of your data file before running a new or untested macro. This prevents accidental corruption of your primary working files. If a macro produces an error, you will have a clean backup to try again.
Troubleshooting
If your macro does not run correctly, check the following common issues:
Macro Not Showing: If the Macros dialog box is empty, ensure you have saved the file as a Macro-Enabled Workbook (*.xlsm). Also, ensure the file is not protected with a password or read-only status.
Run-Time Error 1004: This is the most common error. It usually means the macro is trying to access a range that does not exist. Check the code for cell references and ensure your data sheet matches the sheet referenced in the code. Verify that the sheet name in the code matches the actual tab name in Excel exactly (case-sensitive).
Macro Runs on Wrong Sheet: If the macro runs but affects the wrong spreadsheet, you likely recorded your actions on a different sheet than you intended. Open the VBA Editor, double-click the sheet object in the left pane (e.g., Sheet1), and ensure the code is located there, not in the Module. Alternatively, add Sheets("SheetName").Select at the very beginning of your macro code.
Shortcuts Not Working: If you assign a shortcut key (e.g., Ctrl + Shift + M) but it does not work, ensure no other active Excel workbook is using that specific shortcut combination. You can also try assigning a different key or removing it from the macro definition and adding it via the Quick Access Toolbar instead.
Frequently Asked Questions
Can I share a macro-enabled Excel file with someone who doesn't have Excel?
No. Macros are a proprietary feature of Microsoft Excel. Files containing macros (.xlsm) can only be opened and edited in Excel. If you send the file to someone using Google Sheets, Numbers, or LibreOffice, they will see the data but the macro button will not function.
Is it safe to run macros from unknown sources?
No. Macros can contain malicious code that can damage your files or steal data. Only run macros from sources you trust, such as files you created yourself or documents from your employer. When opening a file that asks if you want to enable macros, choose Disable Macros if you are unsure.
How do I delete a macro?
In Excel, go to the Developer tab and click Macros. Select the macro you want to remove and click Delete. In the VBA Editor, you can also right-click the specific module and choose Remove Module.
Can I use Excel macros on a Mac?
Yes, Excel for Mac supports VBA macros. However, the Developer tab is hidden by default and requires enabling it in Preferences. The functionality is largely the same, though the interface and some menu paths are different from the Windows version.
Why does my macro run very slowly?
Slow macros are often caused by calculating the worksheet during the macro’s execution. You can speed this up by adding the line Application.ScreenUpdating = False at the start of your code and Application.ScreenUpdating = True at the end. This prevents the screen from refreshing as the macro runs, which significantly improves performance.