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.
- 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.
Click on “Options” in the File menu (as shown in the below image).

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

- 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.โ
The worksheet displays the Developer tab, as highlighted in the image below.

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

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.

The steps to add an Excel macro are listed as follows:
- Click the โRecord Macroโ option in the Developer tab.

- 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.

- The โReplaceDotโ macro will start recording the user actions in Excel. The user will observe the โstop recordingโ button appearing in the Developer tab.

- 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.

- The final output is displayed in the below image.

- 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.

- 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.

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.

- The user can choose the type of button to be created. Select the first button fromย โform controlsโย in the Controls tab.

- 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โ. ย

- 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.

- Select the new name list to implement the same task by running macros, as indicated in the previous section.

- Click the โButton 3โ to run the assigned macro โReplaceDotโ.

- To change the button name, use the โEdit textโ option to replace the text โButton 3โ with โReplaceDotโ.

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.

- 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.

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.โ

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.
- System Defined Function –ย It performs actions like creating a link of all the worksheet names, deleting all worksheets and so on.
- 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.

- 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 ( ).โ

- 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.

- The output is displayed in the succeeding image.

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.

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).

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.

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.

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.
Recommended Articles
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 โ


