Open In App

How to Record a Macro in Excel

Improve
Improve
Like Article
Like
Save
Share
Report

Excel Macro is simply a record button that records a set of actions performed on Excel that can be run multiple times. For example, if you have to format some raw data on a weekly basis you can use a macro to record yourself formatting the data once and let Excel automate the task in the future. it’s used to help users save time and avoid performing redundant tasks.

HOW-TO-RECORD-MACRO-IN-MICROSOFT-EXCEL

What is a Macro in Excel

In simple words, a Macro is a snippet of VBA code that empowers you to execute a series of actions with a single command. Whether you’re generating this code using a macro recorder or crafting it manually, it all falls under the umbrella of macros.

How to Get the Excel Developer Tab Show Up in the Ribbon

The first step to record a macro is to get the Developer tab turned on. If you already see the Developer tab in the ribbon, then you can directly follow the steps to record a macro.

How to Enable the Developer Tab in Excel

In order to record a macro the Developer tab on the ribbon needs to be activated as it isn’t on by default within Microsoft Excel. The developer tab contains all the macro tools among various other features. 

How to Activate: To activate it click on the file tab> go to options>Customize Ribbon. Under Customize the Ribbon and under Main Tabs, select the Developer check box. (The developer tab stays on the ribbon unless checked off ).

DeveloperTabGfG

Developer Tab

How to Record a Macro in Excel

Step 1: Select Macro

Click on the view tab, select Macros(extreme right), and hit Record Macro.

Select-Macro

Select Record Macro

Step 2: Record Macro Dialog Box and click Ok

A Record Macro Excel dialog box pops up. The record macro dialog box has 4 sections to be filled:

1. Macro name: Give a name to your macro (avoid spaces and keep it short)

Shortcut Key: you can select any letter to create a shortcut key, be careful not to override an existing shortcut key. (you can press shift and any letter in the box to create a ctrl+shift+letter  key)

2. Store macro in select: This workbook to run it only in the current workbook, personal macro workbook so the macro can be used within any workbook on the computer that contains that personal macro workbook and new workbook to run it in a new workbook it creates at the time of recording.

3. Description: Here you can provide additional information about the macro(what it does) for the users or ourselves.

4. Click ok and Excel will start recording all of your actions.

Select-Option-in-Macro-Dialog-box

Enter the Macro name

Step 3: Stop Recording Macro

Once done, remember to hit the stop recording button in the lower-left corner of the worksheet or head over to view>Macros>stop recording.

Stop-Recording

Stop recording button bottom left corner

Stop-Recording-button

Select Stop-Recording Button

How Macro works in Excel

Now let’s venture into Excel’s Backend, where the magic of macros truly unfolds- the visual basic editor(VB Editor). This is where we’ll dissect what happened when we record a macro and translate it into VBA code.

What is Visual Basic Editor 

Excel has a tool called the Visual Basic Editor or VBA that makes note of all the actions you perform while recording a macro in the backend. When you record a macro inside Excel through the macro recorder all the code gets stored in a module. To see the code that Excel has written head to the Developer tab>code section>Visual Basic this opens up a separate window called the Visual Basic Application window along the left side of this window you will find the module under the Project Explorer(if you don’t see it click on View>Project Explorer) double click it and all the text is the code that excel generated while recording the macro.

vbaGfG

Virtual Basic editor

Alternatively, you can use the keyboard shortcut Alt + F11( holds the Alt key and press F11).

How VB Editors works in Windows

VBA-Window

VBA Window

Menu Bar: This is a ribbon of VBA. You can access the tools and options from here within the VB editor.

Toolbar: Resembling the Quick Access Toolbar, the toolbar offers handy shortcuts. You can even customize it to suit your needs.

Project Explorer Window: This consists of the list of all workbooks and objects within each workbook. For example, if your workbook has 3 worksheets, they’ll be visible here.

Code Window: This is where the VBA code is either recorded or written.

Properties Window: You can view and modify the properties of different objects.

When we recorded the macro- For ‘enter text’, the following is a set of actions that occurred in the VBA editor:

  • A new module was inserted.
  • A macro named “Enter text” was recorded, as per our specifications.
  • The code was penned within the code window of the module.
window-module-gfg

VBA Window Module

What is Absolute Recording and Relative Recording

A macro can be recorded in 2 ways namely Absolute and Relative recording. When you record a macro in Excel it is by default in the Absolute recording mode. In the absolute recording, the steps are performed exactly in the cells the steps were recorded in despite the active cell. In simple words, absolute recording restricts the location of steps. Relative recording executes the steps recorded in any part of the worksheet selected by the user as its relative to the starting location of the macros. Follow the steps to record a macro in Relative reference mode:

Step 1: Select a cell and Click on the Developer tab

Select the cell where you want to insert the macro and Select the developer tab.

Step 2: Select Relative Reference

In the code group, click on the ‘Use Relative Reference’ button. It will turn green, indicating that it’s turned on.

RelativerefrencesGfG

Select Relative Reference

Step 3: Click Record Macro

Click the Record Macro Button.

Record-Macro

Select Record Macro

Step 4: Enter name for your Macro

In the Record Macro Dialog box, enter a name for your macro.

Name-the-Macro

Name the Macro

Step 5 : Select This Workbook and press Ok

In the Store macro option, make sure ‘this workbook’ is selected.

Cons of Excel Macros

Macros have certain limitations pertaining to them and can be combated by writing the VBA code manually. Some of them are as follows

  • A macro doesn’t record what options you select within a dialog box.
  • If you need to make any decisions in your process (code), the macro recorder can’t help you make them. you can use the IF statement or other conditional statements.
  • The macro recorder can’t throw message boxes to prompt the user.

What is an Excel Macro-Enabled Workbook

There are some file extensions that do not store a macro hence you need to be careful about using a macro-enabled format to save your workbook or the macro won’t be saved. 

The default Excel format of the Excel workbook is .xlsx which does not support macros.  To store macros you save and open workbooks in a new macro-enabled workbook format .xlsm. this format is the same as .xlsx but with the capability to support Excel macros.

The Excel Binary Workbook(.xlsb) and Excel macro-enabled template(.xltm) are some more file extensions that support macros within them.

How to Save a Macro in Excel for all Workbooks

Step 1: Navigate to the File Tab and Select Save As

Click on the File tab and then choose Save As. The Save As dialog box appears.

Step 2: Enter the Name for the Workbook

Enter a name for the and select a location for your workbook.

SaveasGfG

Save your Macro

Step 3: Select the File Type

Click the Save as Type drop-down arrow. A list of file types appears.

Step 4: Click on Save

Select Excel Macro-Enabled Workbook. Click on Save.

macroenabled2

Save Macro

Some more real-time applications of macros can be used to record the creation of charts based on some data, filtering and sorting data, formatting data, or importing it.

How to View Macros in Excel

To examine and modify the code of an Excel macro, you can use the Visual Basic Editor. To access the Editor, either press Alt + F11 or click on the Visual Basic button found in the Developer tab.

Screenshot-(159)

The Visual Basic Editor features various windows, but we’ll concentrate on the two primary ones:

Project Explorer: This window displays a list of all currently open workbooks and their respective sheets. It also showcases modules, user forms, and class modules.

Code Window: The Code Window is where you can view, edit, and write VBA code for each object listed in the Project Explorer.

How to Run a Macro in Excel

We already know the basic method of How to Run a Macro using the Macro Dialog Box. But there are other methods also to run a macro in Excel as mentioned:

Run a Macro from the Ribbon( Developer Tab)

Open the workbook with Macro -> Open the Developer tab in the code group -> click Macros -> Now select the Macro you want to run -> press Run.

Using Keyboard Shortcut

Alt +F11 – This key combination will open the Macro Editor in Excel.

Alt +F8 – This key combination will open the Macro Dialog Box.

Ctrl +Shift + M– This key combination will create a new macro in Excel.

Assign the Macro to a Shape

Click on Insert tab in the Ribbon -> Click Shapes in the Illustrations Group -> Select any Shape ->Draw the Shape -> Right click on the shape and select Edit Text -> Type Run Macro -> Format the Text -> Right click on the Shape -> Select Assign Macro from frop down -> Select the Macro.

How to Record a Macro in Mac

Macros in Excel for Mac use VBA (Visual Basic for Applications) just like in Excel for Windows, so the recording process is quite similar.

Step 1: Launch Microsoft Excel on your Mac

Step 2: Enable the Developer Tab (if not already enabled)

Quick Tip: If you don’t see the Developer tab in Excel’s ribbon, you need to enable it first.

Go to Excel -> Preferences in the top menu.

In the Preferences window, click on “Ribbon & Toolbar.”

Check the box next to “Developer” and click “Save.”

Step 3: Open a Workbook

Open the workbook where you want to record the macro, or create a new one.

Step 4: Access the Developer Tab

Click on the “Developer” tab in the Excel ribbon. This tab should now be visible after enabling it in the previous step.

Step 5: Start Recording the Macro

In the Developer tab, click on the “Record Macro” button. This will open the “Record Macro” dialog box.

Step 6: Name Your Macro

In the “Macro name” field, give your macro a descriptive name. Avoid spaces or special characters in the name.

Step 7: Specify a Shortcut (Optional)

You can assign a keyboard shortcut to your macro if desired. This step is optional.

Step 8: Choose Where to Store the Macro

Select where you want to store the macro:

This Workbook: The macro will be available only in the current workbook.

New Workbook: The macro will be available in any new workbooks you create.

Personal Macro Workbook: The macro will be available in all workbooks you open.

Step 9: Click OK to Start Recording

After configuring the options, click the “OK” button to start recording your actions.

Step 10: Perform Your Actions

Excel will now record every action you perform, such as formatting cells, entering data, or any other task you want to automate.

Step 11: Stop Recording

When you’re done with the actions you want to record, return to the “Developer” tab, and click on the “Stop Recording” button.

Your macro is now recorded and ready for use. You can run it at any time by either using the keyboard shortcut you assigned (if any) or by going to the Developer tab and selecting “Macros,” then choosing your macro from the list and clicking “Run.”

Also Read

FAQs

What are Macros in Excel?

A macro is a snippet of VBA code that empowers you to execute a series of actions with a single command. Whether you’re generating this code using a macro recorder or crafting it manually, it all falls under the umbrella of macros.

How to save a workbook with macros?

To save a workbook with macros, follow the below steps:

Choose the “Excel macro- enabled workbook” format from the “save as type”: drop-down menu in the “Save As “dialog box.

Is it possible to edit the recorded macro code?

Yes, you can edit the recorded macro code in the VBA. This allows you to customize and optimize the code to suits your specific needs.



Last Updated : 19 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads