Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

What Are Macros in Excel? Simple Step-by-Step Guide

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Advanced Example: Automating a Monthly Report
  6. Platform Notes
  7. Security Considerations
  8. Automation Beyond Simple Tasks
  9. Testing Your Macro
  10. Version Differences
  11. Frequently Asked Questions
  12. Final Checks Before Sharing

Macros in Excel are small programs that automate repetitive tasks, saving time and reducing errors.

What You'll Need

  • A workbook that contains data you want to process, such as a list of sales figures or a budget sheet.
  • Basic familiarity with Excel's ribbon interface, including the ability to locate the Developer tab.
  • Macro security settings adjusted to allow macros to run; otherwise Excel will block them for safety.

Step-by-Step

  1. Enable the Developer tab: click File > Options > Customize Ribbon, check the box labeled Developer in the right‑hand column, and click OK. The Developer tab now appears next to Home, Insert, Data, and other familiar tabs.
  2. Open the VBA editor: click Developer > Visual Basic, which opens a window titled Microsoft Visual Basic for Applications. In that window, click Insert > Module to create a new, blank code module.
  3. Write a simple macro: type Sub HelloWorld() MsgBox "Hello, World!" End Sub and press F5 to run it. The MsgBox function displays a dialog box with the text you specify, confirming that the macro executed successfully.
  4. Assign a button to run the macro: return to Excel, go to the Developer tab, click Insert, then under Form Controls choose a button shape such as a rectangle. Draw the button on the sheet, right‑click the shape, select Assign Macro from the context menu, choose HelloWorld from the list, and click OK. Clicking the button now triggers the macro.
  5. Save the file as a macro‑enabled workbook: click File > Save As, select Excel Macro‑Enabled Workbook (*.xlsm) from the file type dropdown, give the file a descriptive name such as BudgetAutomation.xlsm, and click Save. If you save as the older *.xlsx format, Excel will strip out all VBA code.
  6. Test the macro on real data: create a small table of numbers, select a range, and record a macro that copies the selected cells to another location. Run the macro to see how it manipulates the data without manual copying.
  7. Explore relative references: when recording a macro, Excel captures the exact cell addresses at the moment of recording. To make the macro work on any selected range, use Relative References, which you toggle on the status bar before starting the recording.
  8. Add comments to your code for future clarity: place an apostrophe (') at the beginning of a line to make it a comment. For example, 'Copy data to another sheet explains the purpose of the following line, helping anyone who reads the code later.
  9. Debug a macro that does not behave as expected: open the VBA editor, set a breakpoint by clicking to the left of a line of code, then press F8 to step through the macro line by line, watching how variables change in the Immediate window.

Common Mistakes

One of the most common errors is forgetting to save the workbook as *.xlsm, which causes all VBA code to disappear the next time the file is opened. Another frequent mistake is using absolute cell references when the macro is intended to work on any range of data; this makes the macro fail when applied to a different section of the sheet. Additionally, some users enable macros globally, which can expose the workbook to malicious code; it is safer to enable macros only for the specific workbook you trust.

Tips

When recording a macro, speak aloud each step you perform; the recorder will translate your actions into VBA statements, giving you a starting point even if you are not comfortable writing code manually. Keep macros focused on a single task, such as formatting a header row or summing a column, which makes them easier to test and maintain. Use meaningful names for macros, such as CopyToSummary, rather than generic names like Macro1, to improve readability and avoid confusion when multiple macros exist.

Advanced Example: Automating a Monthly Report

Suppose you receive a monthly sales report in a sheet named Data, and you need to copy the totals from columns B through D into a summary sheet named Summary each month. The following macro demonstrates the process:

Sub PrepareMonthlyReport()
    Dim src As Worksheet, dst As Worksheet
    Set src = ThisWorkbook.Worksheets("Data")
    Set dst = ThisWorkbook.Worksheets("Summary")
    dst.Range("A1:D1").Value = src.Range("B1:D1").Value
    dst.Range("A2").Value = src.Range("B10").Value
    dst.Range("A3").Value = src.Range("C10").Value
    dst.Range("A4").Value = src.Range("D10").Value
    MsgBox "Monthly report prepared."
End Sub

Explanation of each line:

  • Dim declares variables that store references to worksheets.
  • Set assigns the actual worksheet objects to those variables.
  • dst.Range("A1:D1").Value = src.Range("B1:D1").Value copies a block of cells from the source sheet to the destination sheet.
  • dst.Range("A2").Value = src.Range("B10").Value copies a single cell value.
  • MsgBox displays a confirmation dialog after the operation completes.

To run this macro, add a button on the Summary sheet, assign the PrepareMonthlyReport macro to it, and click the button after the monthly data is entered.

Platform Notes

Macros are only available in the desktop version of Excel on Windows and macOS; the web version and mobile apps do not support VBA code execution. On macOS, the Developer tab may be hidden by default and must be enabled through Excel Preferences > Ribbon & Toolbar. Additionally, macro security settings on macOS are accessed via Excel > Preferences > Security, where you can choose to enable all macros or be prompted for each file.

Security Considerations

Only enable macros from sources you trust. Malicious macros can run commands such as deleting files, sending emails, or installing unwanted software. Excel warns you with a security banner when a workbook contains macros; click Enable Content only if you are certain of the file's origin. If you suspect a macro is harmful, open the VBA editor, locate the module, and delete the code before saving the file again.

Automation Beyond Simple Tasks

Macros can call other macros, interact with external databases, and even control other Office applications such as Word or PowerPoint through Automation. For example, a macro can export a chart as an image file and attach it to an email automatically. However, each additional capability introduces more complexity and potential points of failure, so it is advisable to master basic macros before attempting advanced integrations.

Testing Your Macro

Before deploying a macro across a large workbook, test it on a copy of the data. Verify that the macro performs the intended transformation, that no unintended cells are altered, and that the workbook remains saved as *.xlsm after the macro runs. If the macro crashes, the VBA editor will display an error message that includes the line number and a brief description of the problem, such as "Object required" or "Type mismatch". Use this information to locate and correct the faulty code.

Version Differences

Menu names and ribbon locations can differ slightly between Excel 2016, 2019, 2021, and Microsoft 365. For instance, the option to enable macros may appear under Trust Center Settings > Macro Settings in newer versions, while older versions might label it simply "Disable all macros without notification". When following instructions, look for the general concept rather than an exact wording; the underlying functionality remains the same.

Frequently Asked Questions

Can I run a macro on a Mac?

Yes, VBA macros work on macOS, but the steps to open the VBA editor and the location of the macro security settings differ slightly from Windows.

Do macros work in Excel Online?

No, the web version of Excel does not support VBA macros; it only offers limited scripting capabilities through Power Automate.

How do I view the code behind a recorded macro?

Open the VBA editor, locate the module that contains the macro, and you will see the generated code. You can edit it directly to refine the behavior.

Is there a way to run a macro without a button?

Yes, you can press a keyboard shortcut you assign to the macro, or run it from the Developer tab by clicking Macros, selecting the macro name, and clicking Run.

Will macros affect file size?

Macros themselves do not significantly increase file size, but if they store large amounts of data in variables or create many worksheets, the workbook may grow accordingly.

Final Checks Before Sharing

When you send a macro‑enabled workbook to a colleague, verify that the file still opens with macros enabled on their system, that the macro runs without errors, and that no personal data is exposed in the code. Consider adding a brief read‑me sheet that explains how to enable macros and what the macro does, to prevent confusion.