Open In App

Macros In Excel With Examples: Step-by-Step Tutorial

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Excel macros are a powerful tool that can automate repetitive tasks, saving you time and increasing productivity. Whether you're trying to enable Excel macros, record a macro in Excel, or automate specific actions within your spreadsheet, macros are an invaluable feature. In this guide, we will walk you through the essential steps of using macros in Excel, including how to run the macro in Excel, create macros using the VBA editor, and even how to run a macro by clicking a button. We’ll also show you how to view macros in MS Excel and how to save the recorded macros in Excel for future use. By the end of this article, you'll be equipped with the knowledge to automate tasks and optimize your workflow with ease.

Prerequisites for Using Excel Macros

Before learning how to create macros in Excel you have to turn o the developer mode in Excel. Follow these steps:

Step 1: Open MS Excel

Navigate to the MS Excel icon and give it a click to open it.

Step 2: Right-click on the Ribbon

Go to the menu and and perform a right-click on the ribbon, select Customize the ribbon.

Macros In Excel With Example

Step 3: Check the Developer box and Click on “OK”

Navigate to Customize the ribbon and place a check on the Developer Checkbox

Macros In Excel With Example

How to Enable Excel Macros

To enable macros in Microsoft Excel, follow these steps:

For Excel on Windows:

  1. Open the Excel File:
    • Open the Excel workbook that contains the macros you want to enable.
  2. Access the Trust Center:
    • Click on the File tab in the top-left corner of the screen.
    • Select Options at the bottom of the left-hand menu.
    • In the Excel Options window, select Trust Center from the left-hand pane.
    • Click on the Trust Center Settings button on the right side.
  3. Navigate to Macro Settings:
    • In the Trust Center window, click on Macro Settings on the left.
  4. Choose the Appropriate Option:
    • You will see several options. Select the one that best meets your security needs:
      • Disable all macros without notification: This option disables all macros and does not show any warnings.
      • Disable all macros with notification: Macros are disabled, but you are alerted with a security warning when a macro-enabled file is opened. You can then enable macros manually.
      • Disable all macros except digitally signed macros: This option will enable macros only if they are signed by a trusted publisher.
      • Enable all macros (not recommended; potentially dangerous code can run): This option will enable all macros without any restrictions. Be cautious when using this setting as it can expose you to security risks.
  5. Save Your Settings:
    • Click OK to close the Trust Center window.
    • Click OK again to close the Excel Options window.
  6. Enable Macros for a Specific Workbook (If Prompted):
    • When you open a workbook containing macros, you may see a yellow Security Warning bar below the ribbon. Click Enable Content to allow macros to run.

For Excel on Mac:

  1. Open the Excel File:
    • Open the workbook that you want to enable macros for.
  2. Access the Preferences Menu:
    • Click on Excel in the menu bar and select Preferences.
  3. Select Security:
    • In the Preferences window, click on Security.
  4. Enable Macros:
    • Check the box labeled Enable all macros (not recommended; potentially dangerous code can run).
    • Click OK to save your changes.
  5. Reopen the Workbook (If Necessary):
    • In some cases, you may need to close and reopen the workbook for the changes to take effect.

Important Security Tip:

  • Macros can contain potentially harmful code. It is always best to enable macros from trusted sources only. Consider using "Disable all macros with notification" as your default setting, and then enable macros on a case-by-case basis.

How to Record a Macro in Excel

Recording a macro is one of the easiest ways to get started with Excel Macros:

Step 1: Click on the Developer Tab

Go to the Developer tab located at the ribbon.

Macros In Excel With Example

Step 2: Click on the Record Macro

In the Code Group, the tool Record Macros is located, to excel macros record you have to click on it.

Macros In Excel With Example

Step 3: Fill the Record Macro Dialog Box

As macros are selected, a dialog box is opened which contains the following options to fill.

Macros In Excel With Example

In the dialog box, enter the name of the Excel macro in the Macro name. However, space cannot be used in the Macro name to separate the words, underscore(_) can be used for this purpose. Example: Macro_name.

There are some other options also in the dialog box, i.e., where to store the macro, the shortcut key which can be used to activate the macro, and the description for the macro created. They can be filled accordingly. 

  • After filling in the dialog box, click on OK.
  • As soon as OK is clicked, Excel Macro records the set of operations the user is practicing.
  • Once, the work is done, in the Developer Tab, an option "Stop Recording" can be seen. Clicking on which the recording gets stopped and the created macro is saved.

Macros In Excel With Example

How to Run the Macro in Excel

There are multiple ways to run macros after creating macros in Excel, here we will learn how to run excel macros:

Run the Macro by Clicking on a Shape

It is one of the easiest ways to run a macro to create any shape in the worksheet and use it for running the macro.

Step 1: Click on the Insert Tab on the ribbon

Macros In Excel With Example

Step 2: Choose any Shape

Go to the Illustrations group and click on the Shapes icon. In the sheet, choose any shape we like and want it to be assigned as the macro.

Macros In Excel With ExampleMacros In Excel With Example

Step 3: Add the Shape

Click where you want to add the shape in the sheet and the shape will automatically get inserted.

Macros In Excel With Example

Step 4: Edit the Shape

The shape can be resized or re-formatted accordingly to the way you want. Text can also be added to the shape.

Macros In Excel With Example

Step 5: Right Click on the Shape and Click on Assign Macro

Then Right-Click on the shape and a dialog box would be opened. Then click on Assign Macro.

Macros In Excel With Example

After right-clicking on the shape, another dialog box of Macro would be opened.

Macros In Excel With Example

Step 6: Now Select the Macro

In the dialog box, select the macro from the list you want to assign to your shape and click the OK button. Now the shape would work as a button and whenever you click on it, it will run the assigned macro.

How to Run Macro By Clicking a Button

While the shape is something you can format and play with it, a button has a standard format. A macro can also be assigned to a button for easy use and then can run the macro in Excel by simply clicking that button.

Step 1: Go to the Developer Tab on the Ribbon

Macros In Excel With Example

Step 2: In the Controls group, click on the Button Tool

Macros In Excel With Example

Step 3: Click on the Excel Sheet and Fill the Details in the Dialog box

Now, click anywhere on the Excel sheet. As soon as you do this, the Assign Macro dialog box would be opened.

Macros In Excel With Example

Step 4: Select the Macro you want to Assign and Click OK

Select the macro you want to assign to that button and fill in the other options. Then click on OK.

Step 5: Preview the Shape

After that, a rectangular button can be seen on the sheet, and ready to be used.

Macros In Excel With Example

Step 6: Format the Button

The button which appears on the sheet has default settings, and you can't format it like the color, or shape of the button.

However, the text which appears on the button can be changed. Just a right-click on the button and a dialog box would open. Click on Edit Alt Text and change the text. 

Macros In Excel With Example

How to Create Macro in VBA Editor

While recording macros is a quick way to automate tasks, writing VBA code allows you to create more complex macros with advanced functionality. To write custom VBA macros, follow these steps: 

Step 1: Go to the Developer Tab on the Ribbon

Macros In Excel With Example

Step 2: Click on the Visual Basic

In the Code Group, click on Visual Basic and a new window would be opened.

Macros In Excel With ExampleMacros In Excel With Example

Step 3: Click on Run Macro

Then click on the Run Macro sign, it will open a dialog box listing all the macros of that workbook.

Macros In Excel With Example

Step 4: Select the Macro from the list

Select the macro you want to run from the list and click Run.

As soon as Run is clicked, the macro would be executed on the excel-sheet. If you can only see the VB Editor window and not the sheet, then you may not see the changes happening in the worksheet. To see the changes, minimize/close the VB Editor window. You can also edit a macro as per your need.

Macros In Excel With Example

How to View Macros in MS Excel

To view Macros in Excel follow the steps given below:

Step 1: Open Microsoft Excel

Step 2: Click on the Developer Tab

After opening the Microsoft Excel go the Developer tab.

Macros In Excel With Example

Step 3: Click Macros to see a list of available macros in the active workbook

Step 4: Select a Macro to View

Step 5: You can run or edit the macro from here if needed.

Step 6: Close the Macro dialog when you’re done

Macros In Excel With Example

How to Save the Recorded Macros in Excel

After creating or recording a macro, save your workbook as an Excel Macro-Enabled Workbook (.xlsm) to keep the macro accessible in the future.

Step 1: Record your Actions

To save Excel macro you have to record it first so if you don't how to record Excel macro follow the steps given above.

Select-Macro
Record your Actions

Step 2: Click on Stop Recording

Stop the recording Excel Macro when you are done recording a macro in Excel

Stop-Recording-button
Click on Stop Recording

Step 3: Save your workbook as “.xlsm” (macro-enabled)

When you will save a macro in Excel then it will automatically saved in the workbook.

macroenabled2
Save your workbook as “.xlsm”

Step 4: Excel Macro Saved

Now your macro in excel is saved and you can use it whenever you want.

Example Macros for Practical Use

Macros can help automate a variety of tasks in Excel:

  • Data Formatting: Create a macro to format a dataset automatically.
  • Report Generation: Automate the generation of reports with specific parameters.
  • Sorting and Filtering: Use macros to quickly sort or filter data.

These examples are just the beginning of what Excel macros and Excel VBA can accomplish.

Conclusion

Now that you have a better understanding of macros in Excel, you can begin using them to streamline your workflow and automate time-consuming tasks. Whether you’re recording a macro in Excel, running it by clicking a button, or creating more advanced macros through the VBA editor, these tools will help you work more efficiently. By learning how to view macros in MS Excel and save the recorded macros, you can manage and reuse your automations for improved productivity. With consistent practice, you’ll be able to fully leverage Excel’s macro capabilities to handle complex tasks effortlessly.


Similar Reads