• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • SALE 20% Off All Courses
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Logout
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

How to Record a Macro in Excel

You are here: Home / Excel VBA / How to Record a Macro in Excel
How to record a macro in Excel
June 30, 2016 by Philip Treacy

Prompted by a couple of questions on our Excel forum I thought I'd write about how to record a macro in Excel, and show how simple it can be.

Excel contains a tool called the Macro Recorder, and what it does is to create VBA code for the actions you perform in Excel.

So you can use it to record the steps needed to perform boring and repetitive tasks, and then run the macro later to perform this task for you. By examining the code the macro recorder generates you can teach yourself about VBA too.

The Developer Tab

Before you can record a macro, you need to enable the Developer tab in your Ribbon.

Once that is done you have various tools and buttons available to work with macros.

excel macro recorder example 1

Macro 1 : Center Across Selection

We have some text we want to center across the selected cells. To do this manually you have to:

  • Select the cells to center across
  • Right click on the cells and choose Format Cells
  • Click on the Alignment tab
  • Choose Center Across Selection from the Horizontal Text alignment drop-down list
  • Click OK

Recording the Macro

Type some text into a cell, it doesn't matter what is is for the purposes of recording the macro. Then click on the Developer tab and click on Record Macro.

Excel will pop-up a window and ask you what you want to call the macro, so give it a useful name, then click OK. I'm calling mine CenterAcross.

In the VBA editor, Excel will create a code module for you and insert the new sub (macro). Press ALT+F11 to open the VBA editor and see this all happening in real time.

Now we go ahead and perform the steps listed above to center the text across the selected cells. When we are finished we press Stop Recording.

This is how it looks when I did it. On the left is the VBA editor with the macro recorder generating code, as I work in Excel.

excel macro recorder example 1

Editing the Macro

One of the things with the macro recorder is that is inefficient. In the code it generates it includes lines that actually don't do anything. Looking at the code above you might figure out that it's the line .HorizontalAlignment = xlCenterAcrossSelection that does the centering across the selected cells.

We can comment out almost everything else and the macro still does what we want. But as I already said, by looking at the other lines you can learn VBA. The .WrapText = False controls text wrapping. By changing False to True we can wrap the text in the cells. What do you think .MergeCells = False will do if you set it to True?

So if we comment out all the unnecessary lines we end up with this, which does exactly the same as the original recorded code.

Of course you can just go ahead and delete the lines that are commented out.

excel macro recorder example 1 edited

Macro 2 - Changing Number Format

The 2nd problem posted on the forum was about number formatting. This person entered numbers in the default Accounting format, and then modified this so that there was no currency symbol, and no decimal places.

The image below shows me recording the macro, then running it to format another number.

excel macro recorder example 2

Running The Recorded Macros

Great, we've recorded two macros, we just need to make them easily available so we can run them at any time. There are a couple of ways to do this.

The first is to create an icon on your QAT which runs the macro when it is clicked.

The other is to create a shortcut key-sequence to run the macro.

Using either of these approaches makes it simple to run our macros whenever we want.

Remember to save the file as a macro enabled workbook.

PERSONAL.XLSB

If you want to make the macro available in every workbook you use, then save it into your PERSONAL.XLSB before you start recording it

save macro to personal.xlsb

How to record a macro in Excel

More Excel VBA Posts

Display All Matches from Search in Userform ListBox

Display All Matches from Search in Userform ListBox

Search a range for all partial and full matches of a string, and display matching records (entire rows) in a userform listbox. Sample code and userform.
animating excel charts

Animating Excel Charts

Use animation correctly to enhance the story your data is telling. Don't animate your chart just for some eye candy. Sample code and workbook to download.
dynamic data validation lists in userforms

Dynamic Data Validation Lists in Userforms

Data validation lists using the same source that are dynamically modified to prevent the same choice being made in each list.
show report filter pages for power pivot pivottables

Show Report Filter Pages for Power Pivot PivotTables

PivotTables created from Power Pivot can't use the 'Show Report Filter Pages' option. But this piece of VBA allows you to do just that.
charting real time data in excel

Charting Real Time Data in Excel

Receive data in real time and chart the data as it arrives. Can be used to chart things like stock prices or sensor readings. Sample code and workbook
select multiple items from drop down data validation list

Select Multiple Items from Drop Down (Data Validation) List

Choose multiple items from a data validation (drop down) list and store them all in the same cell. Sample workbook with working VBA.
Excel Calendar (Date Picker) to Use in Worksheets and Userforms

Multi-Language Excel Calendar (Date Picker) for Worksheets and Userforms

Easy to use, highly customizable and multi-language. This date picker is implemented as a userform that is simple to integrate into your workbook.
automating and emailing pivot table reports

Automating and Emailing Pivot Table Reports

Automate the creation and distribution of pivot table reports with some VBA. Send reports to multiple email recipients using Outlook.
search for data with userform

Searching for Data With a User Form

Search a list of records (like a table) using a user form, and then populate the fields of the search form when the record is found.
Checking values in range objects with vba

Checking Values in Range Objects With VBA

Use built in tools or your own code to examine the values contained within Range objects in VBA. Sample code to download.




Category: Excel VBA
Previous Post:Power Pivot does VLOOKUPPower Pivot does VLOOKUP
Next Post:Create Regular Excel Charts from PivotTablesRegular Charts from PivotTables

Reader Interactions

Comments

  1. Joan

    July 28, 2016 at 1:31 am

    You might want to add to your instructions the shortcut keys to view the spreadsheet and VBA code side-by-side (WINKEY + Left Arrow) and (WINKEY + Right Arrow).

    Reply
    • Philip Treacy

      July 28, 2016 at 11:57 am

      Thanks Joan, good tip. I have 2 monitors so don’t use this myself but handy for those with 1 screen.

      Regards

      Phil

      Reply
  2. Joan

    July 2, 2016 at 1:48 am

    I cannot see the macro in the VBA editor. It shows:
    Microsoft Excel Objects
    Sheet1
    ThisWorkbook
    Modules

    Where is the macro located?

    Reply
    • Philip Treacy

      July 2, 2016 at 9:14 am

      Hi Joan,

      Click on Modules and you will see the code modules. Double click on these and you will see the code contained in each one.

      Regards

      Phil

      Reply
  3. Jon Acampora

    July 1, 2016 at 1:34 am

    Great article Phil! I like the center across selection example. I wish there was a button on the ribbon for it, but this is a great example of how simple macros can save us a lot of time. Thanks!

    Reply
    • Philip Treacy

      July 1, 2016 at 8:49 am

      Thanks Jon. Yes there are lots of things like the Center Across example that we can use macros to do for us.

      Cheers

      Phil

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

Course Sale

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Subscribe to Our Newsletter

Receive weekly tutorials on Excel, Power Query, Power Pivot, Power BI and More.

We respect your email privacy

Guides and Resources

  • Excel Keyboard Shortcuts
  • Excel Functions
  • Excel Formulas
  • Excel Custom Number Formatting
  • ALT Codes
  • Pivot Tables
  • VLOOKUP
  • VBA
  • Excel Userforms
  • Free Downloads

239 Excel Keyboard Shortcuts

Download Free PDF

Free Webinars

Excel Dashboards Webinar

Watch our free webinars and learn to create Interactive Dashboard Reports in Excel or Power BI

Click Here to Watch Now

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
trustpilot excellent rating
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved

Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.