Run a macro - Microsoft Support (2024)

Skip to main content

Microsoft

Support

Support

Sign in

Sign in with Microsoft

Sign in or create an account.

Hello,

Select a different account.

You have multiple accounts

Choose the account you want to sign in with.

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 More...Less

There are several ways to run a macro in Microsoft Excel. A macro is an action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language. You can always run a macro by clicking the Macros command on the Developer tab on the ribbon. Depending on how a macro is assigned to run, you might also be able to run it by pressing a combination shortcut key, by clicking a button on the Quick Access Toolbar or in a custom group on the ribbon, or by clicking on an object, graphic, or control. In addition, you can run a macro automatically whenever you open a workbook.

Before you run macros

Before you start working on macros you should enable the Developer tab.

  1. Open the workbook that contains the macro.

  2. On the Developer tab, in the Code group, click Macros.

  3. In the Macro name box, click the macro that you want to run, and press the Run button.

  4. You also have other choices:

    • Options - Add a shortcut key, or a macro description.

    • Step - This will open the Visual Basic Editor to the first line of the macro. Pressing F8 will let you step through the macro code one line at a time.

    • Edit - This will open the Visual Basic Editor and let you edit the macro code as needed. Once you've made changes, you can press F5 to run the macro from the editor.

You can add a combination shortcut key to a macro when you record it, and you can also add one to an existing macro:

  1. On the Developer tab, in the Code group, click Macros.

  2. In the Macro name box, click the macro that you want to assign to a combination shortcut key.

  3. Click Options.

    The Macro Options dialog box appears.

  4. In the Shortcut key box, type any lowercase or uppercase letter that you want to use with the shortcut key.

    Notes:

    • For Windows, the shortcut key for lowercase letters is Ctrl+letter. For uppercase letters, it's Ctrl+Shift+Letter.

    • For Mac, the shortcut key for lowercase letters is Option+Command+letter, but Ctrl+letter will work as well. For uppercase letters, it's Ctrl+Shift+Letter.

    • Be careful assigning shortcut keys, because they will override any equivalent default Excel shortcut key while the workbook that contains the macro is open. For example, if you assign a macro to Ctrl+z, you'll lose the ability to Undo. Because of this, it's generally a good idea to use Ctrl+Shift+Uppercase letter instead, like Ctrl+Shift+Z, which doesn't have an equivalent shortcut in Excel.

    For a list of Ctrl combination shortcut keys that are already assigned in Excel, see the article Excel shortcut and function keys.

  5. In the Description box, type a description of the macro.

  6. Click OK to save your changes, and then click Cancel to close the Macro dialog box.

To run a macro from a button on the Quick Access toolbar, you first have to add the button to the toolbar. To do that, see Assign a macro to a button.

You can create a custom group that appears on a tab in the ribbon, and then assign a macro to a button in that group. For example, you can add a custom group named "My Macros" to the Developer tab, and then add a macro (that appears as a button) to the new group. To do that, see Assign a macro to a button.

Run a macro by clicking an area on a graphic object

You can create a hotspot on a graphic that users can click to run a macro.

  1. In the worksheet, insert a graphic object, such as a picture, or draw a shape. A common scenario is to draw a Rounded Rectangle shape, and format it so it looks like a button.

    To learn about inserting a graphic object, see Add, change, or delete shapes.

  2. Right-click the hotspot that you created, and then click Assign Macro.

  3. Do one of the following:

    • To assign an existing macro to the graphic object, double-click the macro or enter its name in the Macro name box.

    • To record a new macro to assign to the selected graphic object, click Record, type a name for the macro in the Record Macro dialog box, and then click OK to begin recording your macro. When you finish recording the macro, click Stop Recording Run a macro - Microsoft Support (2) on the Developer tab in the Code group.

      Tip:You can also click Stop Recording Run a macro - Microsoft Support (3) on the left side of the status bar.

    • To edit an existing macro, click the name of the macro in the Macro name box, and then click Edit.

  4. Click OK.

On the Developer tab, click Visual Basic to launch the Visual Basic Editor (VBE). Browse the Project Explorer to the module that contains the macro you want to run, and open it. All of the macros in that module will be listed in the pane on the right. Select the macro you want to run, by placing your cursor anywhere within the macro, and press F5, or on the menu, go to Run > Run Macro.

Create a Workbook_Open event.

The following example uses the Open event to run a macro when you open the workbook.

  1. Open the workbook where you want to add the macro, or create a new workbook.

  2. On the Developer tab, in the Code group, click Visual Basic.

  3. In the Project Explorer window, right-click the ThisWorkbook object, and then click View Code.

    Tip:If the Project Explorer window is not visible, on the View menu, click Project Explorer.

  4. In the Object list above the Code window, select Workbook.

    This automatically creates an empty procedure for the Open event, such as this:

    Private Sub Workbook_Open()

    End Sub

  5. Add the following lines of code to the procedure:

    Private Sub Workbook_Open()
    MsgBox Date
    Worksheets("Sheet1").Range("A1").Value = Date
    End Sub

  6. Switch to Excel and save the workbook as a macro-enabled workbook (.xlsm).

  7. Close and reopen the workbook. When you open the workbook again, Excel runs the Workbook_Open procedure, which displays today's date in a message box.

  8. Click OK in the message box.

    Note:The cell A1 on Sheet1 also contains the date as a result of running the Workbook_Open procedure.

Need more help?

You can always ask an expert in the Excel Tech Communityor get support inCommunities.

Top of Page

See Also

Automatically run a macro when opening a workbook

Automate tasks with the Macro Recorder

Record a macro to open specific workbooks when Excel starts

Create and save all your macros in a single workbook

Save a macro

Need more help?

Want more options?

Discover Community

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Run a macro - Microsoft Support (4)

Microsoft 365 subscription benefits

Run a macro - Microsoft Support (5)

Microsoft 365 training

Run a macro - Microsoft Support (6)

Microsoft security

Run a macro - Microsoft Support (7)

Accessibility center

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Run a macro - Microsoft Support (8)

Ask the Microsoft Community

Run a macro - Microsoft Support (9)

Microsoft Tech Community

Thank you for your feedback!

×

Run a macro - Microsoft Support (2024)

FAQs

Run a macro - Microsoft Support? ›

Make a document trusted to enable macros

If you see a security warning when you open a document or try to run a macro, you can choose to make it a trusted document and enable macros. This example is on an Excel workbook. Select Enable Content. In the Security Warning dialog, select Yes to make the document trusted.

Does Microsoft support macros? ›

Make a document trusted to enable macros

If you see a security warning when you open a document or try to run a macro, you can choose to make it a trusted document and enable macros. This example is on an Excel workbook. Select Enable Content. In the Security Warning dialog, select Yes to make the document trusted.

Does Word support macros? ›

In Word, you can automate frequently used tasks by creating and running macros. A macro is a series of commands and instructions that you group together as a single command to accomplish a task automatically.

Is Microsoft discontinuing VBA? ›

No, it is extremely unlikely that Microsoft will ditch VBA. There are far too many solutions all over the world that rely on it.

Can Office 365 run macros? ›

If you have tasks in Excel 365 that you perform repeatedly, you can record a macro to automate those tasks. When creating a macro, you are recording your mouse clicks and keystrokes.

How to fix cannot run macro in Excel? ›

Step 1: Click on "File" > "Options." Step 2: Go to "Trust Center" > "Trust Center Settings." Step 3: Select "Macro Settings" and choose "Enable all macros" (not recommended for security reasons) or "Disable all macros with notification." Step 4: Click "OK" to save changes.

How to unblock macros in Microsoft Access? ›

Step 1 – Launch MS Access. Click on “Options”: Step 2 – Click on “Trust Center”, then on “Trust Center Settings”, “Macro settings”, select “Enable all macros” and click OK: Step 3 – Exit MS Access.

Is Microsoft removing macros? ›

VBA macros are a common way for malicious actors to gain access to deploy malware and ransomware. Therefore, to help improve security in Office, we're changing the default behavior of Office applications to block macros in files from the internet.

Has Microsoft stopped supporting VBA? ›

Office 2010 includes VBA 7.0. There are no new features in VBA 7 for developers compared to VBA 6.5 except for 64-bit support. However, after VBA 6.5/Office 2007, Microsoft stopped licensing VBA for other applications. Office 2013, Office 2016, Office 2019 and Office 2021 include VBA 7.1.

Can you run macros on Windows? ›

Before you run macros

For Windows, go to File > Options > Customize Ribbon. For Mac, go to Excel > Preferences... > Ribbon & Toolbar. Then, in the Customize the Ribbon section, under Main Tabs, check the Developer check box, and press OK.

Top Articles
Best Community Colleges in Maryland
✅ Logística de Coca-Cola: Qué es y cómo funciona [2022]
Live2.Dentrixascend.com
Uconn. Bursar Office
R Statistical Software
Fhsd Lunch Menu
Cvs Stage And Covington Pike
Lisas Stamp Studio
Contact Spectrum Customer Service
Kino finden | cinetixx Filme
Abigail Letts O'brien Obituary
Wjet Tv News
Union Corners Obgyn
Fox News 10 Mobile Al
Craigslist Virginia For Sale
Caroline G. Atkinson Intermediate School
Newburyport Rockport Line
Best Rally Car Games
Tom DiVecchio - LILLY BROADCASTING | LinkedIn
Wmlink/Sspr
IBM i Access Client solution
Nier: Automata - How to Get All Endings
Charm City Kings 123Movies
2022 Chevy Malibu Gas Button
Daily Press Escanaba Mi Obituaries
866-392-8015
Yale College Confidential 2027
What Channels Are Included In Spectrum Entertainment View
Morgandavis_24
Walker Medical Diagnostics Patient Portal
Mendoza Clinic Pharmacy
Black Panther 2 Showtimes Near Regal Treasure Coast Mall
Cheap Motorcycles For Sale Under 1000 Craigslist Near Me
Bad Moms 123Movies
Kathy Carrack
Rugged Gentleman Barber Shop Martinsburg Wv
Bòlèt Florida Midi 30
How to Sell Cars on Craigslist: A Guide for Car Dealers | ACV Auctions
Keylee Morales
Monroeville Craigslist
Stellaris Ultima Vigilis
Bbwchan Blueberry
10 Funniest Non Sequitur Comics, Ranked
Guadalajara Taqueria Cisco Menu
Sharkbrew
Tiger Island Hunting Club
Vlad The Impaler Dick Size
Purplefoxy840127
Rise Menu Dispensary
Behind the Casefiles - Dnepropetrovsk Maniacs - Eileen Ormsby
Jeff Monreal Funeral Home Obituaries
Whyp It Downloader
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5825

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.