How to Record a Macro in Excel?

To record a macro in excel, either the View tab or the Developer tabDeveloper TabEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more can be used, as shown in the following images.

Let us consider an example to understand the steps to create an excel macro.

The following table contains the overall grades of students. The report is to be formatted on a daily basis. We need to perform tasks like applying borders, highlighting headers, setting background colors, and so on.

We want to record a macro for these repetitive tasks.

Using Recorded Macros

There is another table containing the grades of students. It has to be formatted in the same way the data of example #1 was formatted.

  • In the Developer tab, select “record macro” under the “code” section. The “record macro” dialog box opens, as shown in the following image. In “macro name,” enter “formatting.” Note: It should be ensured that no space is entered while naming the macro. In “shortcut key,” assign the shortcut key “F” to the macro. This shortcut runs the macro. In “store macro in,” select “this workbook.” With this selection, the macro is stored in the present workbook. Moreover, this macro will work only in the current workbook. In “description,” type what the macro intends to do. Click “Ok” and the “record macro” option changes to “stop recording” under the “code” group. At this time, the actions we perform are recorded in the macro. To ensure that the automated tasks are executed every time a new range is selected, activate “use relative references.” This is done prior to creating the macro. Once the steps of formatting like applying borders, highlighting headers, setting background colors, etc., have been recorded, click “stop recording” (shown in the sixth point). Apply recorded formatting with the help of the shortcut key “Ctrl+Shift+F.” The output is shown in the following image.

Note: It should be ensured that no space is entered while naming the macro.

Select A1 and press “Ctrl+Shift+F” for formatting the selected cells. Hence, custom formatting is applied in one go without carrying out multiple commands.

The limitation of the macro recorder is that it is full of unnecessary codes.

The Cautions While Recording a Macro

The following points must be remembered while recording a macro:

  • The file containing macros must be saved with the “.xlsm” extension.While downloading an Excel file with the “.xlsm” extension (macro-enabled workbook), it should be ensured that the source is authentic.The “auto sum” function should not be used while creating a macro because it hard codes the formula.The formula should be typed with a single dollar sign like “=SUM(E$2: E10).” The macro recorder records E$2 as a fixed reference and begins the SUM directly below the “row 1” headings. It recognizes E10 as a relative reference and points directly above the current active cell E11.

Frequently Asked Questions

To avoid doing the repetitive tasks again and again, Excel provides a way of automating them. The steps like cell formatting, number formatting, saving workbooks, deleting worksheets, importing data, and so on are recorded as macros. A macro is created using the macro recorder and can be replayed whenever required. The macro can be run, edited, copied (in another workbook), assigned (to a button or object), enabled, and disabled. The Excel macro code can be viewed and edited in Visual Basic Editor (VBE).

The tips to record macros in Excel are listed as follows: • Relative referencing must be activated to ensure that the macro works with new selections. • Record a macro only if you are familiar with the steps. If the shortcut key “Ctrl+Z” is pressed, it is recorded. So, a smooth recording is essential for running the macro efficiently. • A backup of the workbook must be created before running a macro. This helps prevent unwanted changes to the current file. • Instead of a large macro, create several short ones. This is because shorter macros are easy to understand and allow error-free recording.

The two references are explained as follows: – If a macro is recorded using absolute references, the VBA code will always refer to the same cell selection. This selection is used every time a macro is run, irrespective of the current active cell. For instance, cell A2 is selected, text “hello” is typed, and the “enter” key is pressed. The code will always begin by selecting cell A2. – If a macro is recorded using relative references, the VBA code will record the movement rather than the exact cell selection. Working on the preceding example, if cell K2 is selected, Excel will move relative to cell K2. So, Excel will not work on cell A2.

Key Takeaways

  • Recording macros is a method whereby Excel stores the tasks performed by the user.To run a macro implies that the exact recorded actions are executed automatically.A macro can be recorded with the help of either the View tab or the Developer tab.No space should be entered while naming the macro.Prior to creating a macro, “use relative reference” must be activated to switch on relative referencing.The file containing macros must be saved with the “.xlsm” extension.

This has been a guide to Record a Macro in Excel. Here we learn how to create a Macro in Excel along with Excel example and downloadable Excel templates. You may also look at these useful functions of Excel –

  • Excel VBA Active CellExcel VBA Active CellThe active cell is the currently selected cell in a worksheet. Active cell in VBA can be used as a reference to move to another cell or change the properties of the same active cell or the cell’s reference provided from the active cell.read moreAdd Developer Tab ExcelAdd Developer Tab ExcelEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read moreMacros in ExcelMacros In ExcelA 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.
  • read moreVBA MacrosVBA MacrosVBA Macros are the lines of code that instruct the excel to do specific tasks, i.e., once the code is written in Visual Basic Editor (VBE), the user can quickly execute the same task at any time in the workbook. It thus eliminates the repetitive, monotonous tasks and automates the process.read moreExponents in ExcelExponents In ExcelIn Excel, exponents are the same exponential function as in mathematics, where a number is raised to a power or exponent of another number. Exponents can be used by two methods: the power function or the exponent symbol on the keyboard.read more