Elite Membership

Macros in Excel

Written by Vidya Subbu Vidya Subbu Excel Content Writer & Editor Vidya, a former software engineer turned seasoned content writer with 7+ years of experience, excels in creating engaging content. As an editor at WallStreetMojo, she dreams of publishing her own book. 7+ years of experience B.E. (Electronics and Instrumentation) MS Excel - Basic and Advanced View Full Profile
Reviewed by Dheeraj Vaidya, CFA, FRM Dheeraj Vaidya, CFA, FRM Content Reviewer & Course Director Dheeraj is a former J.P. Morgan and CLSA Equity Analyst with nearly two decades of experience in financial modeling, valuation, equity research, and corporate finance. He specializes in helping students and professionals develop practical and in-demand finance skills through structured and AI-powered, 20+ Years of experience CFA, FRM, IIT Delhi, IIM Lucknow Financial Modeling View Full Profile
Updated Oct 16, 2025
Read Time 9 min

What Is MACRO in Excel?

A macro in Excel is a series of instructions in the form of code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data.ย  For example, it can be used to automate repetitive tasks such as summation, cell formatting, information copying, etc. thereby rapidly replacing repetitious operations with a few clicks.

Download FREE Macros Excel Template and Follow Along!
Download Excel Template
  • The first is when you can record the macro, where Excel records every step automatically and then repeats it.ย 
  • The second is coding with VBA, which requires good subject knowledge

To gain a deeper understanding of how Excel macros can simplify accounting workflows and financial reporting, consider exploring an advanced Excel learning platform. Beforeย recording a macro, the user has to activate the Developer tab in Excel. The Developer tab is a built-in option in Excel to create macros, generate VBA applications, design forms, import or exportย XML files, etc. Since it is disabled in Excel by default, it has to be enabled before creating and recording the macros.

Let us learn the method to enable the Developer tab in Excel.

Key Takeaways

  • A macro in Excel is a series of instructions in the form of code that helps automate repetitive tasks like formatting and performing data imports.
  • To enable macros, go to File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings. Now, choose โ€œEnable all macros.โ€
  • Users can access the code using the keyboard shortcut โ€œAlt+F11โ€ or by editing the macro that was created earlier.
  • To disable a macro, go to File -> Options. -> Trust Center > Trust Center Settings and select Macro Settings. Choose โ€œDisable all macros without notificationโ€ or โ€œDisable all macros with notification.โ€

Enable The Developer tab

Below are the steps to activate the Developer tab in the Excel toolbar.

  1. Click on “Options” in the File menu (as shown in the below image).


    Activate developer Tab step 1

  2. On clicking “Options,” the “Excel options” window will pop up.


    Activate developer Tab step 2

  3. Select “Customize Ribbon” in the “Excel Options” which provides a list of options in a dialog box.
    Under “Customize Ribbon“, select “Main Tabs” under the drop-down โ€œChoose Commands from.โ€.ย 

Among the list of checkboxes, select “Developer” and click “OK.โ€

  1. The worksheet displays the Developer tab, as highlighted in the image below.


    Activate developer Tab step 4

  2. The user can view options like “Visual Basic“, “Macros“, “Record Macro“, etc. on the ribbon of Developer tab (as displayed in the image below).


    Activate developer Tab step 5

Examples Of Macros In Excel

Let us understand how to add macros in Excel with the help of the following examples.

Example #1

A list with different names is available in the table below. Some names have the โ€œ.โ€ symbol. We wish to replace the โ€œ.โ€symbol withโ€œ_โ€ using macros in Excel.

Macro Example 1

The steps to add an Excel macro are listed as follows:

  • Click the โ€œRecord Macroโ€ option in the Developer tab.
Macro Example 1-1
  • The โ€œRecord Macroโ€ window will pop out. Name the macro โ€œReplaceDotโ€ย in the โ€œmacro nameโ€ box.ย To assign a keyboard shortcut, type โ€œCtrl+qโ€ in the โ€œshortcut keyโ€ box.
  • Select the option โ€œThis Workbookโ€ in the โ€œStore macro inโ€ box, which will ensure the macro is stored in the particular workbook.

It is optional to fill the โ€œdescriptionโ€ box explaining the task. Finally, click the โ€œOkโ€ button.

Macro Example 1-2
  • The โ€œReplaceDotโ€ macro will start recording the user actions in Excel. The user will observe the โ€œstop recordingโ€ button appearing in the Developer tab.
Macro Example 1-3
  • Let us now start replacing the โ€œ.(dot)โ€ in the names with โ€œ_(underscore)โ€ by using the โ€œfind and replaceโ€ option. Enter โ€œ.โ€ in the โ€œfindโ€ and โ€œ _โ€ in the โ€œreplaceโ€ option, respectively. Then click the โ€œreplace allโ€ button.

Note: Use the shortcut key โ€œCtrl+Hโ€ to use the โ€œFind andย Replaceโ€ option.

  • The โ€œreplace allโ€ option replaces all the โ€œ.โ€ (dots) with the โ€œ_โ€ (underscores). The number of replacements and the resulting output is shown in the succeeding image.
Macro Example 1-5
  • The final output is displayed in the below image.
Macro Example 1-6
  • In the end, click the โ€œstop recordingโ€ button on the Developer tab to stop the macro recording.

Example #2

We want to run the same task for a new list of names (displayed in the below image). We will run the macro โ€œReplaceDot,โ€ created in theย Developerย tab.

Macro Example 1-7
  • Select the โ€œEnable macroโ€ option from theย Excel ribbon under Developer to view the list of macros created in the Macro window. The users can choose and run the macros based on their requirement.

The succeeding image shows the result of macros running on the new list of names.

Macro Example 1-8

Adding The Macro Button – How To Run Macro By Clicking A Button?

Let us assign a button to the macro instead of choosing the โ€œEnable macroโ€ option.

There are many groups like Add-ins, Controls, and XML under the Developer ribbon.

Macro Example 1-9
Macro Example 1-10
  • Drag the selected button anywhere in the Excel sheet. The โ€œAssign Macroโ€ dialogue box opens. The macros to be assigned are listed in the โ€œMacro nameโ€ box.
  • Select the macro โ€œReplaceDot,โ€ appearing in the list and click โ€œOKโ€. ย 
Macro Example 1-11
  • A button appears in the worksheet. Right click the button and use โ€œedit textโ€ option to change the button text as โ€œButton 3.โ€ It is created on the right-hand side of the sheet, as shown in the below image.
Macro Example 1-12
  • Select the new name list to implement the same task by running macros, as indicated in the previous section.
Macro Example 1-13
  • Click the โ€œButton 3โ€ to run the assigned macro โ€œReplaceDotโ€.
Macro Example 1-14
  • To change the button name, use the โ€œEdit textโ€ option to replace the text โ€œButton 3โ€ with โ€œReplaceDotโ€.
Macro Example 1-15

Using the above steps, we can create, record, and assign the macro for various tasks and automate it.

How To View The Code Of Macros?

The users can view the code for a recorded macro. Excel generates the code based on the steps carried out while recording the macro.

We can access the code using the shortcut โ€œAlt+F11โ€ or by editing the macro that was created earlier.

Let us view the code for the โ€œReplaceDotโ€ macro using the following steps:

  • Open the worksheet that contains the โ€œReplaceDotโ€ macro. In the View tab, click the option โ€œmacros.โ€ Select the โ€œReplaceDotโ€ macro from the list and click the โ€œeditโ€ button.
Macro Example 1-16
  • The โ€œMicrosoft Visual Basic for Applicationsโ€ will be launched.ย  The user can write or edit the code using this application. The below image shows the code of the โ€œReplaceDotโ€ macro.
Macro Example 1-17

Creating Macro By Writing VBA Code

Before writing the VBA code, let us understand the โ€œheadโ€ and โ€œtailโ€ of macros, which are the โ€œSubโ€ and โ€œEnd Sub.โ€

Macro Example 2-1

The user places the lines of VBA codes in the keyword โ€œSubโ€. It executes the instructions in the code. The โ€œEnd Subโ€ keyword stops the execution of the โ€œSub.โ€

Generally, there are two types of macros.

  1. System Defined Function –ย  It performs actions like creating a link of all the worksheet names, deleting all worksheets and so on.
  2. User-Defined Function – To create a User Defined Function (UDF) in macro, the user uses the โ€œfunction and end functionโ€ as the โ€œheadโ€ and โ€œtailโ€ of the codes.

Note: A function returns a value, whereas the Sub does not.

A macro is written on the Visual Basic Editor (VBE) of the โ€œMicrosoft Visual Basic for Applicationsโ€.

Let us learn the steps to write a simple macro in the VBA.

  • Click the โ€œmodule 1โ€ in the โ€œModuleโ€ properties displayed on the left-hand side panel of the VBE window, and start writing the macro.
Macro Example 2
  • Begin with โ€œSubโ€ followed by the macro name and end with โ€œEnd Subโ€. The code is written between the โ€œsubโ€ and โ€œend sub.โ€

The below image shows the โ€œSubโ€ and โ€œEnd Subโ€ for the macro named โ€œsimplemacro ( ).โ€

Macro Example 2-1
  • Write code to display text in the message box. The โ€œMsgBoxโ€ displays an input text message. All text in VBA should be enclosed in double quotes.

For example,ย  the code:ย MsgBox โ€œGood Morningโ€ (shown in the below image) displays the message โ€œGood Morningโ€ in the text box.

Macro Example 2-2
  • The output is displayed in the succeeding image.
Macro Example 2-3

Hence, the same macro can be assigned buttons to automate the task.

How To Save The Recorded Macro In Excel?

After recording, the user saves the macro to reuse in any other worksheet in the future.

Let us follow the below-mentioned steps to save the macro:

  • In the macro-enabled workbook,ย  click โ€œSave As.โ€
  • Select the โ€œExcel macro-enabled workbookโ€ option in the โ€œSave as typeโ€ box while saving the file.
  • Finally, save the macros with the โ€œ.xlsmโ€ file extension.
Save Macro

The guidelines for saving the macro names are stated as follows:

  • Make sure that the name of recorded macros should start with letters (alphabets) or underscore.
  • Use letters, numeric, and underscore characters.
  • Avoid space, symbols, or punctuation marks.
  • Maintain a maximum length of about 80 characters.

Note: When the user saves a macroโ€™s name with space, Excel issues a warning (as shown in the succeeding image).

Macro Warning

How To Enable โ€œMacro Security Settingsโ€?

In this section, let us learn to enable the โ€œMacros security settingsโ€.

When a user opens a workbook containing macros, a security warning – โ€œMacros have been disabledโ€ is displayed under the ribbon.ย Choose the โ€œEnable Contentโ€ option in the box.

Macro Security Warning

To eliminate the security warning, we need to change the โ€œtrust center settingsโ€ by using the following steps:

  • Under the โ€œTrust Center settingsโ€ in the File options, click โ€œMacro settings.โ€
  • You will get different options likeย 
    • Disable all macros without notification:
    • Disable all macros with notification:ย 
    • Disable all macros except digitally signed macros:
    • Enable all macros (not recommended; potentially dangerous code can run):ย 
  • Choose the button based on your security settings. Here, we choose, โ€œDisable all macros with notification.โ€
  • The security is enabled in the โ€œMacro Securityโ€ of the Developer ribbon.
Macro Security

Note 1: The user can create absolute macros (functioning from cell A1) that help to reuse the macro in other worksheets.

Note 2:ย The usage of directional keys (rather than a mouse) for navigation in macros is reliable to add, delete, and change the data in the worksheet.

Frequently Asked Questions (FAQs)

What are macros in Excel?

Macros are a set of simple programs or instructions to automate the common and repetitive tasks performed in the Excel worksheet. It can be recorded, saved, and run multiple times as per the user’s requirement.

It is a time-saving tool involved in preparing data reports and manipulating data carried out frequently in a worksheet.

How to enable macros in Excel?

The users can eliminate the security warning in the worksheet and perform macros in Excel enable by using the following steps:

  • Click “Options” in the File tab.
  • Select “Trust center” in the “Options” window.
  • Choose the option “Trust center settings”.
  • Click “Macro settings” on the left side of the navigation pane.
  • Select “Enable all macros” and click “Ok”.
What is the difference between macro and VBA?

The difference between macro and VBA is stated as follows.

  • Macros in Excel are programming codes that function in the Excel worksheet to perform automated and repetitive tasks. It saves the user’s time and extends the efficiency of Excel.
  • Visual Basic for Applications (VBA) is a programming language of Excel used for creating macros.

This has been a tutorial to Macros in Excel. Here we discuss how to add Macros in Excel along with , along with practical examples and a downloadable template. You may also look at theseย useful functions in Excel โ€“