Recording and Running Macros in Excel
One of the easiest ways to start learning Excel VBA is by using the Macro Recorder.
The Macro Recorder allows Excel to record the actions you perform in a workbook and automatically convert many of those actions into VBA code. You can then view, run, and modify the generated VBA code in the Visual Basic Editor.
- What an Excel macro is
- What the Macro Recorder does
- How to enable the Developer tab
- How to record your first macro
- How to stop recording a macro
- How to run a recorded macro
- How to view the VBA code generated by Excel
- How to edit recorded VBA code
- How to use relative references
- How to use absolute references
- How to assign a macro to a button
- How to use keyboard shortcuts for macros
- How to save macro-enabled workbooks
- Common Macro Recorder mistakes
- Practical exercises
Table of Contents
- What is a Macro?
- What is the Macro Recorder?
- Why Use the Macro Recorder?
- Enable the Developer Tab
- Prepare Your Workbook
- Record Your First Macro
- Understanding the Macro Name
- Macro Description
- Stop Macro Recording
- Run Your First Macro
- View Recorded VBA Code
- Understanding Recorded VBA Code
- Edit Recorded VBA Code
- Absolute Cell References
- Relative Cell References
- Relative Reference Example
- Assign Macro to a Button
- Assign a Keyboard Shortcut
- Personal Macro Workbook
- Saving Macro-Enabled Workbooks
- Macro Security
- Limitations of Macro Recorder
- Macro Recording Best Practices
- Practice Exercises
- Summary
1. What is a Macro?
A macro is a series of instructions that can automate tasks in Excel.
Instead of manually performing the same steps again and again, you can create a macro that performs those steps automatically.
Example
Suppose every day you perform the following tasks:
- Open a report.
- Format the headings.
- Make the headings bold.
- Change the background color.
- Adjust column widths.
- Apply number formatting.
Instead of repeating these steps manually, you can record them once as a macro and run the macro whenever required.
2. What is the Macro Recorder?
The Macro Recorder is an Excel tool that records many actions you perform in the workbook and converts those actions into VBA statements.
For example, if you record yourself:
- Selecting a cell
- Typing text
- Making the text bold
- Changing the font color
- Changing the background color
Excel creates VBA code representing those actions.
This makes the Macro Recorder an excellent tool for beginners who are learning how Excel VBA works.
3. Why Use the Macro Recorder?
The Macro Recorder is useful for several reasons.
1. Learn VBA Syntax
You can perform an Excel action and then look at the VBA code generated by Excel.
2. Automate Repetitive Tasks
Tasks that you perform repeatedly can often be automated.
3. Discover VBA Properties and Methods
The recorded code can show you how Excel represents particular actions using VBA.
4. Create a Starting Point
You can record a macro and then simplify or improve the generated code manually.
4. Enable the Developer Tab
The Macro Recorder is available from Excel's Developer tab.
If you cannot see the Developer tab, you may need to enable it through Excel's Ribbon customization settings.
Developer Tab Contains Tools Such As:
- Visual Basic
- Macros
- Record Macro
- Use Relative References
- Insert Controls
- Macro Security
5. Prepare Your Workbook
Before recording your first macro, create a small practice table.
Enter the following data:
| Cell | Value |
|---|---|
| A1 | Employee Name |
| B1 | Department |
| C1 | Salary |
| A2 | John |
| B2 | Sales |
| C2 | 50000 |
We will record a macro that formats the headings in row 1.
6. Record Your First Macro
Let's create your first recorded macro step-by-step.
Excel is now recording your actions.
7. Understanding the Macro Name
When you record a macro, Excel asks you to provide a macro name.
For example:
FormatHeaders
A good macro name should describe the purpose of the macro.
Good Macro Names
- FormatHeaders
- CreateReport
- CleanData
- FormatSalesData
- PrepareMonthlyReport
Avoid Names Like
- Macro1
- Test123
- ABC
- MyMacro
8. Macro Description
Excel also provides a field for a macro description.
You can write something like:
Formats the header row of the sales report.
Descriptions become especially useful when a workbook contains many macros.
9. Stop Macro Recording
After completing the actions you want to record, you must stop the recorder.
Your actions have now been converted into VBA code.
10. Run Your First Macro
After recording the macro, let's run it.
Method 1: Developer Tab
Method 2: VBA Editor
Open the VBA Editor using:
Alt + F11
Place your cursor inside the macro and press:
F5
Method 3: Keyboard Shortcut
If you assigned a shortcut key while recording the macro, you can use that shortcut to run it.
11. View Recorded VBA Code
One of the most useful learning techniques is to record a macro and immediately examine the VBA code generated by Excel.
You will see VBA code similar to:
Sub FormatHeaders()
Range("A1:C1").Select
Selection.Font.Bold = True
Selection.Interior.Color = RGB(0, 176, 80)
Selection.Font.Color = RGB(255, 255, 255)
End Sub
The exact code generated can vary depending on the actions you recorded and your Excel version.
12. Understanding Recorded VBA Code
Let's examine the example:
Range("A1:C1").Select
This selects cells A1 through C1.
Next:
Selection.Font.Bold = True
This makes the selected cells bold.
Then:
Selection.Interior.Color = RGB(0, 176, 80)
This changes the background color of the selection.
Finally:
Selection.Font.Color = RGB(255, 255, 255)
This changes the font color to white.
13. Edit Recorded VBA Code
Recorded code is not necessarily the final code you should use. You can modify it manually.
Recorded Version
Sub FormatHeaders()
Range("A1:C1").Select
Selection.Font.Bold = True
Selection.Interior.Color = RGB(0, 176, 80)
End Sub
You can often simplify the code by removing unnecessary Select and Selection statements.
Improved Version
Sub FormatHeaders()
With Range("A1:C1")
.Font.Bold = True
.Interior.Color = RGB(0, 176, 80)
End With
End Sub
14. Absolute Cell References
By default, the Macro Recorder often records specific cell locations. These are known as absolute references.
For example:
Range("A1").Select
This code specifically refers to cell A1.
If the macro always needs to work with A1, this may be perfectly fine.
Example
Sub FormatTitle()
Range("A1").Font.Bold = True
Range("A1").Font.Size = 18
End Sub
15. Relative Cell References
Relative references allow the recorded macro to work relative to the current active cell.
This can be extremely useful when you want the same macro to perform an operation relative to wherever you start.
On the Developer tab, Excel provides the:
option while recording macros.
16. Relative Reference Example
Suppose you start from cell A1 and record an action that moves one cell to the right.
Depending on the recording settings, Excel can record movement relative to the starting cell rather than always referring to one fixed address.
This makes the macro more flexible.
17. Assign a Macro to a Button
You can make a macro easier to run by assigning it to a worksheet button.
Using a Form Control Button
Now users can click the button to run the macro.
Example Button
When the user clicks the button, the macro can automatically format the report.
18. Assign a Keyboard Shortcut
You can assign a keyboard shortcut while recording a macro.
For example, you might assign:
Ctrl + Shift + F
to a formatting macro.
19. Personal Macro Workbook
Excel also provides a special workbook called the Personal Macro Workbook.
Macros stored there can be available whenever you use Excel, rather than being limited to one particular workbook.
This can be useful for general-purpose macros that you use regularly.
Example
Suppose you create a macro that formats selected cells in a standard way. You may want to use that macro in many different workbooks.
Storing it in the Personal Macro Workbook can make it available across your Excel sessions.
20. Saving Macro-Enabled Workbooks
If your workbook contains VBA macros, use a macro-enabled workbook format when saving it.
If you save a macro-containing workbook as a normal .xlsx file, Excel cannot retain the VBA project in that file format.
21. Understanding Macro Security
Excel includes security features designed to protect users from potentially harmful macros.
When opening a workbook containing macros from an untrusted source, Excel may display a security warning.
Only enable macros when you understand and trust the source of the workbook and its contents.
22. Limitations of the Macro Recorder
The Macro Recorder is powerful for beginners, but it has limitations.
1. It Records Actions
The recorder primarily records what you do in Excel rather than understanding the business goal behind those actions.
2. Recorded Code Can Be Long
The recorder may generate more code than is actually necessary.
3. Select and Selection
Recorded macros often contain unnecessary Select and Selection statements.
4. Limited Logic
The recorder does not automatically create sophisticated programming structures such as:
- Complex If statements
- Loops
- Error handling
- Custom functions
- Advanced object-oriented programming
These are topics you will learn by writing VBA manually.
23. Macro Recording Best Practices
Best Practice 1: Plan Before Recording
Decide what you want the macro to accomplish before pressing Record Macro.
Best Practice 2: Avoid Unnecessary Actions
Every unnecessary action can become part of the recorded procedure.
Best Practice 3: Use Descriptive Names
Prefer:
FormatSalesReport
instead of:
Macro1
Best Practice 4: Review the Generated Code
Always open the VBA Editor and study the code created by the recorder.
Best Practice 5: Remove Unnecessary Code
As you learn VBA, simplify recorded procedures and remove unnecessary selections.
Best Practice 6: Test Your Macro
Always test the macro on sample data before using it on important workbooks.
24. Practice Exercises
Exercise 1: Record a Formatting Macro
Create a worksheet with the following headings:
Name | Department | Salary | Joining Date
Record a macro that:
- Makes the headings bold.
- Changes the background color.
- Changes the font color.
- Centers the headings.
Exercise 2: Study the Generated Code
Open the VBA Editor and find the macro you recorded.
Identify:
- Sub
- End Sub
- Range
- Select
- Selection
- Font
- Interior
Exercise 3: Improve the Code
If your recorded code contains:
Range("A1:C1").Select
Selection.Font.Bold = True
try changing it to:
Range("A1:C1").Font.Bold = True
Run both versions and verify that they produce the same result.
Exercise 4: Create a Report Formatting Macro
Create a worksheet containing a small sales report.
Record a macro that:
- Formats the heading row.
- Adjusts column widths.
- Formats the salary or sales column.
- Adds borders to the table.
- Centers appropriate headings.
Then open the VBA Editor and study the generated code.
25. Summary
In this lesson, you learned how to record and run Excel macros and how the Macro Recorder can help you learn VBA.
- A macro is a sequence of automated Excel instructions.
- The Macro Recorder converts many Excel actions into VBA code.
- The Developer tab contains the Macro Recorder.
- You can give your macro a descriptive name and description.
- After recording, you must stop the recorder.
- Recorded macros can be run from the Macros dialog.
- You can run VBA procedures using F5 from the VBA Editor.
- The generated VBA code can be viewed and edited.
- Recorded code may contain Select and Selection statements.
- Absolute references refer to specific cell addresses.
- Relative references work relative to the starting position.
- Macros can be assigned to worksheet buttons.
- Macros can also be assigned keyboard shortcuts.
- The Personal Macro Workbook can store reusable macros.
- Macro-enabled workbooks normally use the .xlsm format.
- You should only enable macros from trusted sources.
- The Macro Recorder is a great learning tool, but manual VBA coding provides much greater flexibility.
Next Lesson: Understanding VBA Syntax and Structure
Now that you know how to record and run macros, the next step is to understand the basic structure and syntax of VBA code.
In the next lesson, you will learn:
- VBA statements
- Keywords
- Objects
- Properties
- Methods
- Arguments
- Procedures
- Variables
- Comments
- Basic VBA coding conventions
0 Comments