blog-1
BLOG

What are Macros in Excel? How to Create and Use Them with Examples

Card image cap

Microsoft Excel is widely used for organising data, performing calculations, preparing reports and analysing business information. However, many Excel tasks involve repeating the same steps every day or every month. This is where Excel macros become useful.

An Excel macro is a recorded sequence of actions or a set of VBA instructions that automates repetitive tasks in a worksheet. Instead of manually formatting data, applying calculations or preparing reports every time, you can run a macro to perform those actions automatically.

For example, if you regularly prepare monthly sales reports, you can create a macro to format headings, apply borders, adjust column widths and highlight important figures with a single command.

Whether you are a student learning Excel, a business analyst handling reports or a professional preparing for data analyst interviews, understanding macros can help you work more efficiently.

In this guide, you will learn what Excel macros are, how to create and record them, how to use VBA code, and how to apply macros to practical tasks.

What is a Macro in Excel?

A macro in Excel is a tool that automates a sequence of actions. It records the steps you perform in a worksheet or uses VBA (Visual Basic for Applications) code to complete a task automatically.

Once created, a macro can be run whenever you need to repeat the same process. You can execute it from the Developer tab, assign it a keyboard shortcut or connect it to a button in your worksheet.

Example: Imagine that you receive a sales report every month. You need to make the headings bold, add background colours, apply borders and adjust column widths. Instead of repeating these steps manually, you can record them in a macro and run it whenever a new report arrives.

Key Features of Excel Macros

  • Task automation: Automate repetitive activities such as formatting, calculations and data organisation.
  • Time savings: Complete routine tasks faster by executing multiple actions together.
  • Consistency: Apply the same formatting and process each time a macro is run.
  • Customisation: Use VBA code to create more advanced automation.
  • Reusable workflows: Save macros in a workbook or Personal Macro Workbook for future use.

Why are Excel Macros Important in Data Analysis?

Excel is an important tool for data analysis across industries such as finance, marketing, healthcare, retail and operations. Analysts use it to organise datasets, identify patterns, perform calculations and prepare reports.

However, working with large datasets often involves repetitive activities. For example, an analyst may need to clean data, remove unnecessary formatting, calculate totals or prepare monthly performance reports.

Excel macros help automate these routine activities, allowing professionals to spend more time interpreting data and generating insights.

Here are some important benefits of using macros in data analysis:

  • Efficiency: Automate repetitive calculations, formatting and data-processing tasks.
  • Accuracy: Reduce the chances of manual errors when the same process is repeated.
  • Consistency: Follow a standard process across multiple reports and worksheets.
  • Productivity: Spend less time on routine spreadsheet activities and more time analysing results.
  • Scalability: Use VBA to handle more complex tasks involving multiple worksheets or workbooks.

To understand how information is organised and processed, read our guide on Data Types Explained: From Basics To Advanced.

What are the Different Types of Excel Macros?

Excel macros can be created in different ways depending on the complexity of the task and the user's technical knowledge.

The two common approaches are:

1. Recorded Macros

A recorded macro captures the actions you perform in Excel and converts them into VBA instructions.

You do not need to write code manually. Simply start recording, perform the required actions and stop recording. Excel saves those steps as a macro that you can run again.

Example: Recording the steps required to format a sales report, including bold headings, cell borders and column adjustments.

2. VBA Macros

VBA macros are created or edited using Visual Basic for Applications, the programming language used by Excel for desktop automation.

Unlike recorded macros, VBA macros allow users to write custom instructions, apply conditions, create loops and automate more complex tasks.

Example: Writing VBA code to identify blank cells in a dataset, highlight missing values or calculate totals across multiple worksheets.

What is VBA in Excel?

VBA stands for Visual Basic for Applications. It is a programming language used to automate tasks and customise applications such as Microsoft Excel.

Excel uses VBA to create and run macros. Users can write VBA instructions directly in the Visual Basic Editor or modify the code generated by the Macro Recorder.

For example, the following VBA code displays a message when the macro runs:

Sub MyFirstMacro()

    MsgBox "Welcome to Excel Macros!"

End Sub

When you run this macro, Excel displays a message box containing the text.

VBA is particularly useful when you want to automate tasks that require more than a simple sequence of recorded actions.

What is the Use of Macros in Excel? Practical Examples

Excel macros can be used for several business and data-management activities. Here are some common examples:

1. Formatting Data Automatically

Suppose you receive a spreadsheet every week and need to apply the same formatting to its headings and columns.

A macro can automatically:

  • Make the heading row bold.
  • Apply a background colour to the headings.
  • Add borders to the dataset.
  • Adjust column widths.
  • Align text and numbers.

This helps maintain a consistent format across reports.

2. Cleaning and Organising Data

Large datasets may contain blank rows, inconsistent formatting or duplicate information.

Macros can automate specific data-cleaning steps, such as:

  • Removing completely blank rows.
  • Applying consistent text formatting.
  • Highlighting missing values.
  • Identifying duplicate entries.
  • Standardising selected columns.

Always review the dataset and test the macro on a copy before applying changes to important information.

3. Preparing Monthly Reports

Finance and business teams often prepare reports using the same format every month.

A macro can automate repetitive steps such as calculating totals, formatting tables, highlighting important figures and preparing summary sections.

4. Performing Repetitive Calculations

If you regularly calculate commissions, sales totals or departmental expenses, a macro can automate the required calculations and apply them to the relevant cells.

5. Working Across Multiple Worksheets

VBA can automate activities across multiple worksheets within a workbook. For example, a macro can collect selected information from different department sheets and compile it into a summary sheet.

How to Turn On the Developer Tab in Excel

Before creating or recording a macro, you need to enable the Developer tab. It contains tools for recording macros, opening the VBA Editor and managing macro settings.

Follow these steps in Excel for Windows:

  1. Open Microsoft Excel.
  2. Click File and select Options.
  3. Choose Customize Ribbon.
  4. Under Main Tabs, find and select the Developer checkbox.
  5. Click OK.

The Developer tab will now appear on the Excel ribbon.

Note: The steps may differ slightly depending on your Excel version and operating system.

How to Create a Macro in Excel: Step-by-Step Guide

You can create a simple macro using Excel's built-in Macro Recorder. For beginners, this is one of the easiest ways to understand how automation works.

Let's create a macro that formats a sales report.

Step 1: Open Your Excel Workbook

Open the Excel workbook containing the data you want to format. For this example, assume your sales report contains columns for product name, quantity, price and total sales.

Step 2: Open the Developer Tab

Click the Developer tab on the Excel ribbon. In the Code section, select Record Macro.

Step 3: Enter the Macro Details

A Record Macro dialogue box will appear. Enter the following details:

  • Macro name: FormatSalesReport
  • Shortcut key: Choose a shortcut if required.
  • Store macro in: Select This Workbook if you want the macro to remain in the current file.
  • Description: Add a short explanation of what the macro does.

Click OK to start recording.

Step 4: Perform the Required Actions

Now perform the actions you want Excel to record. For this example:

  1. Select the heading row.
  2. Make the text bold.
  3. Apply a background colour.
  4. Add borders to the data.
  5. Adjust the column widths.

Excel will record the actions as you perform them.

Step 5: Stop Recording

Once you have completed the formatting, return to the Developer tab and click Stop Recording.

Your macro has now been created.

Step 6: Run the Macro

To test your macro, go to Developer > Macros, select FormatSalesReport and click Run.

Excel will execute the recorded steps.

How to Record a Macro in Excel

Recording a macro is useful when you want Excel to capture a series of actions without writing VBA code.

Follow this quick process:

  1. Open the workbook in Excel.
  2. Select the Developer tab.
  3. Click Record Macro.
  4. Enter a suitable macro name.
  5. Assign a shortcut key if needed.
  6. Choose where you want to store the macro.
  7. Click OK and perform the actions.
  8. Select Stop Recording when finished.

Tip: Use descriptive macro names so that you can identify their purpose later. For example, use FormatMonthlyReport instead of a generic name such as Macro1.

You can also use the Use Relative References option on the Developer tab when you want recorded actions to work relative to the currently selected cell rather than always referring to the same fixed cell addresses.

How to Use Macros in Excel

Once you have created a macro, you can run it whenever you need to repeat the automated task.

Here are the common ways to execute a macro:

1. Run a Macro from the Developer Tab

  1. Open the workbook containing the macro.
  2. Go to the Developer tab.
  3. Click Macros.
  4. Select the macro you want to execute.
  5. Click Run.

Excel will perform the actions saved in that macro.

2. Run a Macro Using a Keyboard Shortcut

If you assigned a shortcut key while creating the macro, you can use that shortcut to run it.

For example, if you assigned a shortcut to your report-formatting macro, pressing that key combination will execute the recorded steps.

3. Run a Macro Using a Button

You can also assign a macro to a button in your worksheet.

  1. Open the Developer tab.
  2. Select Insert.
  3. Under Form Controls, choose Button.
  4. Draw the button on your worksheet.
  5. Select the macro you want to assign.
  6. Click OK.

You can change the button's displayed text to something meaningful, such as Format Report. Clicking it will run the assigned macro.

How to Create a Macro Using VBA Code

If you want to automate more complex activities, you can write VBA code directly instead of relying only on the Macro Recorder.

Let's understand how to create a simple VBA macro that formats a sales report.

Steps to Create a VBA Macro

  1. Open your Excel workbook.
  2. Go to the Developer tab.
  3. Click Visual Basic to open the Visual Basic Editor.
  4. In the editor, select Insert > Module.
  5. Enter your VBA code in the module.
  6. Save the workbook in the Excel Macro-Enabled Workbook (.xlsm) format.

Example: VBA Code to Format a Sales Report

Suppose your sales data is stored in columns A to D, with the headings in the first row.

You can use the following VBA code to make the headings bold, apply a background colour, add borders and automatically adjust column widths.

Sub FormatSalesReport()

    With Range("A1:D1")

        .Font.Bold = True

        .Interior.Color = RGB(217, 225, 242)

    End With

    Range("A1:D10").Borders.LineStyle = xlContinuous

    Columns("A:D").AutoFit

End Sub

How this code works:

  • Sub FormatSalesReport() defines the name of the macro.
  • Range("A1:D1") selects the heading cells.
  • .Font.Bold = True makes the headings bold.
  • .Interior.Color applies a background colour.
  • Range("A1:D10").Borders.LineStyle = xlContinuous adds borders to the selected range.
  • Columns("A:D").AutoFit adjusts the widths of columns A to D.
  • End Sub marks the end of the macro.

Note: This example is designed for a report with headings in row 1 and data in rows 1 to 10. Adjust the ranges to match your worksheet.

How to Save Recorded Macros in Excel

If you want to retain your macros after closing Excel, you must save the workbook in a format that supports VBA.

The standard .xlsx format does not store VBA macro code. Instead, use the Excel Macro-Enabled Workbook (.xlsm) format.

Follow these steps:

  1. Click File and select Save As.
  2. Choose the location where you want to save the workbook.
  3. Open the Save as type dropdown.
  4. Select Excel Macro-Enabled Workbook (*.xlsm).
  5. Click Save.

Your workbook will now retain its VBA macros.

If you want to make a macro available whenever you open Excel on your computer, you can store it in the Personal Macro Workbook (PERSONAL.XLSB).

How to Enable Macros in Excel Safely

Excel uses security settings to control whether macros can run. This is important because VBA macros can contain malicious code.

By default, Excel commonly uses a setting that disables macros with a notification, allowing users to decide whether to trust a particular file.

To review your macro settings in Excel for Windows:

  1. Open Excel and click File.
  2. Select Options.
  3. Go to Trust Center.
  4. Click Trust Center Settings.
  5. Select Macro Settings.
  6. Review the available security options.

Important: Avoid selecting "Enable all macros" as a general setting. Microsoft identifies this option as potentially dangerous because it can allow malicious code to run. Only run macros from sources you trust, and follow your organisation's IT security policies.

Files downloaded from the internet may also have their macros blocked by Office security features. Do not bypass these protections unless you have verified the file and its source.

How to View, Edit and Delete Macros in Excel

After creating macros, you may need to review their code, make changes or remove macros that are no longer required.

How to View a Macro in Excel

  1. Open the workbook.
  2. Go to the Developer tab.
  3. Click Macros.
  4. Select the macro you want to view.
  5. Click Edit to open its VBA code in the Visual Basic Editor.

How to Edit a Macro in Excel

To edit a macro, open it in the Visual Basic Editor and modify the relevant VBA instructions.

After making changes, save the workbook and test the macro using a copy of your data. This helps ensure that the updated code works as expected.

How to Delete a Macro in Excel

  1. Open the Developer tab.
  2. Click Macros.
  3. Select the macro you want to remove.
  4. Click Delete.
  5. Confirm the deletion if prompted.

If the macro is stored in a different workbook or in the Personal Macro Workbook, make sure you select the correct location before removing it.

What is the Difference Between VBA Macros and Office Scripts?

Excel offers more than one way to automate spreadsheet tasks. VBA macros and Office Scripts are two important options, but they are not the same technology.

Feature

VBA Macros

Office Scripts

Programming language

Visual Basic for Applications (VBA)

TypeScript

Main use

Desktop Excel automation

Cloud-based and cross-platform automation

Recording actions

Supported through Macro Recorder

Supported through the Action Recorder

Excel for the web

Cannot create or run VBA macros

Supported

Power Automate integration

Not directly supported as an Office Scripts action

Supported

File format

Commonly stored in .xlsm workbooks

Stored and managed through supported Microsoft 365 experiences


Which One Should You Use?

VBA is useful when you work primarily with desktop Excel and need to automate complex spreadsheet tasks. Office Scripts are useful when you want cloud-based automation or workflows that integrate with Power Automate.

The right option depends on your Excel environment, the type of automation required and your organisation's Microsoft 365 setup.

Can You Run Excel Macros in Excel for the Web?

No. Excel for the web can open and edit workbooks that contain VBA macros, but it cannot create, run or edit VBA macros.

If you need to execute VBA macros, open the workbook in the Excel desktop application.

For browser-based automation, Office Scripts may be a suitable alternative, depending on your Microsoft 365 account and feature availability.

Excel Macros and Data Analyst Interview Questions

Knowledge of Excel macros can be useful when preparing for data analyst interviews because employers may assess your ability to automate repetitive spreadsheet tasks.

Here are some common questions you may encounter:

1. What is a macro in Excel, and why is it useful?

A macro is a recorded sequence of actions or a set of VBA instructions that automates repetitive tasks in Excel. It helps save time, improve consistency and reduce manual work.

2. What is VBA in Excel?

VBA stands for Visual Basic for Applications. It is the programming language used to create and customise macros in desktop Excel.

3. What is the difference between a recorded macro and a VBA macro?

A recorded macro captures actions performed by a user in Excel and generates VBA code. A VBA macro is created or modified by writing code directly, allowing more complex automation.

4. How do you save a workbook containing macros?

A workbook containing VBA macros should generally be saved in the .xlsm format, also known as an Excel Macro-Enabled Workbook.

5. How would you use a macro to clean a dataset?

A macro can automate data-cleaning activities such as removing blank rows, highlighting missing values, standardising text and identifying duplicate entries. The exact steps depend on the dataset and the required cleaning rules.

6. How can you troubleshoot a macro that is not working?

First, check whether the workbook is saved in a macro-enabled format and whether its security settings allow the macro to run. Then open the VBA Editor, review the code and use debugging tools such as breakpoints and step-by-step execution to identify errors.

7. What is the difference between VBA macros and Office Scripts?

VBA macros use Visual Basic for Applications and are designed primarily for desktop Excel automation. Office Scripts use TypeScript and support cloud-based automation across supported Excel environments.

For additional interview preparation, explore Top 50 Big Data Interview Questions and Answers for Freshers in 2024.

You can also read 10 Time Saving Advanced Excel Tips To Make You A Pro [2024] to learn more about improving your Excel skills.

MBA in Business Analytics and Data Science: Build Your Data Analysis Skills

Excel macros are one example of how technology can simplify business operations and improve data management. However, modern business analytics involves much more than spreadsheet automation.

Professionals working in analytics need to understand data interpretation, statistical methods, visualisation, business intelligence and decision-making. Learning tools such as Excel, SQL and data visualisation platforms can help students develop practical skills for working with business data.

An MBA in Business Analytics and Data Science can provide a foundation in analytical thinking and help students understand how data supports business decisions.

The Bengal Institute of Business Studies (BIBS), one of the MBA colleges in Kolkata, offers an MBA in Business Analytics and Data Science in collaboration with IBM. This two-year postgraduate programme, affiliated with Vidyasagar University, combines management education with analytical concepts and practical industry-oriented learning.

Conclusion

Excel macros make it easier to automate repetitive spreadsheet activities, from formatting reports and cleaning datasets to performing calculations. By learning how to record macros and write basic VBA code, users can improve their productivity and manage routine data-related tasks more efficiently.

For students and aspiring data analysts, understanding Excel macros is a useful step towards developing practical spreadsheet and automation skills. As business analytics continues to rely on data-driven decision-making, learning tools such as Excel can help build a strong foundation for more advanced analytical applications.

Frequently Asked Questions

1. Is it easy to learn macros in Excel?

Yes. Beginners can start by recording simple macros without writing any code. As they become familiar with the process, they can learn VBA to create more advanced automation.

2. Which programming language is used to create macros in Excel?

Excel macros use Visual Basic for Applications (VBA). It allows users to automate tasks, write custom instructions and control different spreadsheet operations.

3. How do you edit a macro in Excel?

Go to Developer > Macros, select the macro you want to change and click Edit. Excel will open the Visual Basic Editor, where you can modify the VBA code.

4. How can you fix a macro that is not working in Excel?

Check whether the workbook is saved in .xlsm format and whether the macro is allowed to run under your security settings. If the problem continues, review the VBA code in the Visual Basic Editor and use debugging tools to identify errors.

5. Can you use macros in Excel without VBA?

Yes. You can use Excel's Macro Recorder to capture actions without writing VBA code yourself. However, the recorded macro is still saved as VBA code.

6. Can Excel macros work in Excel for the web?

No. Excel for the web can open and edit workbooks containing VBA macros, but it cannot create, run or edit those macros. You need the Excel desktop application to execute VBA macros.

7. What is the difference between an XLSX and an XLSM file?

An .xlsx file is a standard Excel workbook that does not store VBA macro code. An .xlsm file is a macro-enabled workbook that can store VBA macros.

8. Can Excel macros automate multiple worksheets?

Yes. VBA macros can perform actions across multiple worksheets and workbooks. This is useful when consolidating information, preparing summary reports or applying consistent formatting to several sheets.

Copyright - BIBS Kolkata

| Website by Marko & Brando

All rights reserved