Macro run macro excel vba

Did you know you can run other macros from a macro?  This is a very efficient practice that allows us to reuse macros, so we don’t have duplicate code snippets throughout our macros.

This helps us keeps our macros shorter and easier to manage.  We can have several macros calling another macro.

Run A Macro from Another Macro with the Call Statement in VBA Excel

VBA Example: Run Another Macro from a Macro

Here is an example of how to run another macro from a macro using the Call Statement.

Just type the word Call then space, then type the name of the macro to be called (run).  The example below shows how to call Macro2 from Macro1.

It’s important to note that the two macros DO NOT run at the same time.  Once the Call line is hit, Macro2 will be run completely to the end.  Once it is complete, Macro1 will continue to run the next line of code under the Call statement.

Sub Macro1()

    'Place code here to run before calling Macro2
    
    'The following line will run Macro2
    Call Macro2
    
    'When Macro2 is complete the code will continue
    'to run below the call line in this macro

    'Place code here to run AFTER Macro2 runs
    
End Sub

-------------------------------------------------------

Sub Macro2()

    'Place code for Macro2 here

End Sub

Another thing to note is that you do not always have to use the Call statement to call another macro.  You can leave the word Call out, and just type in the macro name.

However, you will need the Call statement if your macro contains parameters (variables) that you want to pass through to the called macro or function.

My personal opinion is that it is best to always use the Call statement when calling another macro.  It may be a few extra letters to type, but it will really help you and others read your code more quickly when debugging it in the future.

Example: Call the Refresh All Pivot Tables Macro from Other Macros

I use this technique of calling other procedures a lot. One common task is to refresh all the pivot tables in the workbook. In the code sample below there are two macros that are both calling the macro to refresh all the pivot tables in the workbook.

Sub Macro1()

    'Code to update the source data
    
    'Run the refresh pivots macro from this macro
    Call Refresh_All_Pivots
    
    'Additional code here

End Sub
-------------------------------------------------------
Sub Macro2()

    'Code to update formulas
    
    'Run the refresh pivots macro from this macro
    Call Refresh_All_Pivots
    
    'Additional code here
    
End Sub
-------------------------------------------------------
Sub Refresh_All_Pivots()
'Refresh all pivot tables caches in the active workbook
Dim i As Integer

    For i = 1 To ActiveWorkbook.PivotCaches.Count
        ActiveWorkbook.PivotCaches(i).Refresh
    Next i

End Sub

Return to VBA Code Examples

This tutorial will demonstrate how to call a macro from another macro in VBA.

Call a Macro From a Macro

So you just recorded two macros, and you would like to run them as one macro, it’s pretty simple.

Assuming you have Macro1 and Macro2, put this code at the end of Macro1

Sub Macro1 ()
Call Macro2
End Sub

Now every time you run Macro1, Macro2 runs automatically. Macro1 will wait until Macro2 is finished before continuing to run.
To run the macros simultaneously use Application.Run method:

Application.Run

You can also use Application.Run to call a macro.

Sub Macro1 ()
Application.Run Macro2
End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!

<<Return to VBA Examples

In VBA, there are several ways to run a macro from a macro, and even run a macro from another workbook. On forums, these methods are used interchangeably depending on the commenter’s personal preferences. This can lead to confusion for anybody trying to learn VBA. Depending on the method for running/calling the macro, the syntax is slightly different.

This post shows these common methods to run and macro from a macro and how to use them.

There are 3 common methods for calling a macro contained in the same workbook.

  • Using the macro’s name only
  • Calling the macro
  • Run a macro

These 3 methods are detailed below.

Use the macro’s name

In this code example, only the macro’s name is required.

Sub CallAnotherMacro()

NameOfMacro

End Sub

When using this method with parameters/arguments, the first argument follows the name of the macro, each subsequent argument is separated by a comma.

Sub CallAnotherMacro()

Dim argument1 As String
Dim argument2 As Integer
argument1 = "ExampleText"
argument2 = 1

NameOfMacro argument1, argument2

End Sub

Calling a macro

Using the Call statement is my preferred method. It is easy to read because it is clear that another macro is being run.

Sub CallAnotherMacro()

Call NameOfMacro

End Sub

Parameters used with this method are enclosed in parentheses and separated by a comma.

Sub CallAnotherMacro()

Dim argument1 As String
Dim argument2 As Integer
argument1 = "ExampleText"
argument2 = 1

Call NameOfMacro(argument1, argument2)

End Sub

Run a macro

The Run command is similar to Call, but requires the name of the macro to be in the form of a text string.

Sub CallAnotherMacro()

Run "NameOfMacro"

End Sub

To use arguments, the text string is followed by a comma, each argument is separated by a comma.

Sub CallAnotherMacro()

Dim argument1 As String
Dim argument2 As Integer
argument1 = "ExampleText"
argument2 = 1

Run "NameOfMacro", argument1, argument2

End Sub

As the name of the macro is a text string, this can be treated like a variable. Therefore the code can run different macros depending on the scenario. This is detailed in the section below.

Run a macro based on a variable

It is possible to Run a macro based on the value of a string variable. This enables a different macro to be triggered depending on the value of the string variable at that point.

In the example below:

  • NameOfMacro is the name of the Sub procedure (the same as the examples above)
  • MacroName is a string variable, which holds the text “NameOfMacro”
Sub CallAnotherMacro()

Dim MacroName As String

MacroName = "NameOfMacro"

Run MacroName

End Sub

In the code above, the Run command is not followed by a text string, but by a variable. The Run function works because the variable is a text string.

To use parameters using this method the syntax is as follows:

Sub CallAnotherMacro()

Dim MacroName As String
Dim argument1 As String
Dim argument2 As Integer

MacroName = "NameOfMacro"
argument1 = "ExampleText"
argument2 = 1

Run MacroName, argument1, argument2

End Sub

Run a macro contained in another workbook

Note: The workbook which contains the macro must be open. If you need to open the workbook first, check out the code in this post.

To run a macro contained in another workbook, use the Application.Run command as follows:

Sub CallAnotherMacro()

Application.Run "'Another Workbook.xlsm'!NameOfMacro"

End Sub

The single quotation marks around the workbook name are required when the workbook name contains a space.

If there is no space, the single quotation marks are optional. The code will still run correctly if they are included. I prefer to use them for consistency and reducing risk if the code were changed.

Sub CallAnotherMacro()

Application.Run "AnotherWorkbook.xlsm!NameOfMacro"

End Sub

Run a macro contained in another workbook based on a variable

Using the text string variable, we can build the name of the workbook and macro. This enables us to call any macro from any open workbook.

Sub CallAnotherMacro()

Dim WorkbookName As String
Dim MacroName As String
WorkbookName = "AnotherWorkbook.xlsm"
MacroName = "NameOfMacro"

Run "'" & WorkbookName & "'!" & MacroName

End Sub

If using parameters, these can also be included as follows:

Sub CallAnotherMacro()

Dim WorkbookName As String
Dim MacroName As String
Dim argument1 As String
Dim argument2 As Integer
WorkbookName = "AnotherWorkbook.xlsm"
MacroName = "NameOfMacro"
argument1 = "ExampleText"
argument2 = 1

Run "'" & WorkbookName & "'!" & MacroName, argument1, argument2

End Sub

Calling and running macros from Workbook, Worksheet or UserForm Modules

All the examples above assume the VBA code is stored within a Standard Module. But, VBA code can be held within Workbook, Worksheet, and UserForm Modules too. To call a macro from one of these types of modules, it requires a small adjustment; the name of the object must precede the name of the macro.

Using the same Run method as above, to run a macro contained within the Workbook Module the code would be as follows:

Sub CallAnotherMacro()

Run "ThisWorkbook.NameOfMacro"

End Sub

Notice that NameOfMacro has now become ThisWorkbook.NameOfMacro.

If the VBA code is contained within the Workbook Module of another workbook, the code would be adjusted as follows:

Sub CallAnotherMacro()

Run "'AnotherWorkbook.xlsm'!ThisWorkbook.NameOfMacro"

End Sub

Use the code name of the object to reference a Worksheet Module or UserForm Module.

Conclusion

There are many ways to run a macro from a macro. The variety of methods can cause a lot of confusion as different users prefer different methods. Hopefully, this post has given you the knowledge to do this for yourself.

Related Posts:

  • Automatically run a Macro when opening a workbook
  • Private vs Public Subs, Variables & Functions in VBA
  • Useful VBA codes for Excel (30 example macros + Free ebook)

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn’t until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).


Do you need help adapting this post to your needs?

I’m guessing the examples in this post don’t exactly match your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Written by Puneet for Excel 2007, Excel 2010, Excel 2013, Excel 2016, Excel 2019, Excel for Mac

1. Run a Macro from the List

From the Developer Tab, you can access the list of the MACROS, which you have in your workbook or in PERSONAL.XLSB. To run a macro in Excel, you can use the below steps:

  1. Click on the macro button from the “Developer Tab” and open the list of macros.
    1-run-a-macro-from-the-list
  2. In this list of MACROS, you will have all the macro you have in the open workbooks, including the Personal Macro Workbook.
    2-personal-macro-workbook
  3. Just select the macro you want to run and click on the “RUN” button.

When you click on the run button, it executes the macro and closes the dialog box.

2. Run a Macro with a Shortcut Key

You can also run a macro using a keyboard shortcut key. Usually, when you record a macro, it asks you to define a shortcut key that you can use to run that macro.

And if you are writing a macro, you can define a shortcut key from the list of macros.

  1. Select the name of the macro for which you want to define the shortcut key and click on the options.
    4-select-the-name-of-the-macro
  2. After that, click within the input box and press the shortcut key that you want to define.
    5-click-in-the-input-box

3. Add a Macro Button to Quick Access Toolbar

You can also add a button to the Quick Access Toolbar to run a macro. You can use the below steps:

  1. First, click on the small dropdown that you have on the quick access toolbar and select more commands, and it will take you to the actual options to customize the quick access toolbar.
    6-add-a-macro-button-to-quick-access-toolbar
  2. Now from here, select the macros from the truth command from and select the macro that you want to add, after that click on the add button and it will add that macro to do quick access toolbar.
    7-customize-the-quick-access-toolbar
  3. In the end, click, OK.

And you will have a button for the macro that you have added.

4. Add Macro to a Shape

Let’s say you have a VBA code which you need to use frequently in your work. In this situation, you can create a button and assign that macro to it.

  1. First, insert a simple shape from Insert Tab ➜ Illustrations ➜ Shapes. Select any of the shapes which you want to use as a button.
    9-add-macro-to-a-shape
  2. After that, right-click on that shape and select “Assign Macro”.
    10-right-click-on-that-shape
  3. Now from the list of macros, select the macro which you want to assign to the shape.
    11-list-of-macros

Now, whenever you click on that shape, the macro which you assigned will execute.

5. Assign a Macro to a Form Control Button

Apart from using a shape, you can also use a control button to run a macro.

  1. First, go to the Developer tab and in the controls group and then click on insert. And from the insert drop-down, click on the button to insert it.
    12-assign-a-macro-to-a-form-control-button
  2. After that, it will show you the macros list from where you can select it.
    13-assign-macro-dialog-box
  3. Once you select the macro and click OK, you will get a button in the worksheet (you can change the text of the button to give it a meaningful name).
    14-click-ok-you-will-get-a-button

6. Opening and Closing a Workbook

You can also make a macro to run while opening and closing a workbook. That means when you open or close a workbook, the macro you have assigned will get executed. For this, you need to use “auto_open” and “auto_close”.

Let’s suppose you want to assign a macro to run while opening the workbook. You need to use auto_open as the name of that macro.

Sub auto_open()
Range("A1").Value = Now
End Sub

Now, this micro will run when you open the workbook and enter the current date, and type in the cell A1 of the active sheet.

In the same way, you can also use “auto_close” to make this macro while closing the workbook.

7. Activating and Deactivating a Worksheet

Just like the workbook can also run a macro on activating and deactivating a worksheet. And in this case, you need to add that macro into the code window of that worksheet.

  1. First, right-click on the worksheet tab and click on the “view code”.
    16-activating-and-deactivating-a-worksheet
  2. Now in the code window, select the worksheet from the left drop-down. The moment you chose deactivate; you’ll get a new sub with the name “Worksheet_Deactivate”.
    17-select-the-worksheet-from-the-left-dropdown
  3. Now you need to add the code in this procedure that you want to run when you deactivate the worksheet.
Private Sub Worksheet_Deactivate()
Range(“A1”).Value = Now
End Sub

And if you want to run a macro when you activate a worksheet, select activate instead of deactivated from the drop-down.

Private Sub Worksheet_Activate()
Range(“A1”).Value = Now
End Sub

8. Run a Macro When a Change in Worksheet

You can also run a macro when you make changes to a worksheet. For example, when you enter a value in a cell or delete a value from a cell.

For this, you, again, need to enter the good in the code window of the worksheet and select “Selection Change” from the drop-down.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range(“A1”).Value = “Last Updated: ” & Format(Now, “dd-mmm-yy hh:mm:ss Am/pm”)
End Sub

9. Within Another Procedure

You can run a macro from another procedure by using the call statement. Use the keyword Call and then the name of the macro.

Sub myStrikeThrough()
If Selection.Value = “Yes” Then
Selection.Value = “No”
Else
Selection.Value = “Yes”
End If
End Sub

Sub markDone()
Call myStrikeThrough
Selection.Font.Bold = True
End Sub

When you run “markDone” macro, it first runs “mystrikethrough” macro and then makes the selection font bold.

10. Schedule a Macro

You can also schedule a macro to run at a specific time. Let’s suppose if you want to run a macro in the morning at 8:30, you can specify the time, and we will run it.

For this, you need to use an Application.OnTime method. Let’s suppose you have a macro “myCode”, you can write the code like below to run it at 8:30 AM.

Application.OnTime TimeValue("08:30:00"), "myCode"

[icon name=”bell” class=”” unprefixed_class=””] VBA is one of the Advanced Excel Skills

Do you want to learn how to run a VBA macro in Microsoft Excel? This guide will show you all the methods you can use to run your VBA code.

Microsoft Excel is a powerful spreadsheet application that offers users a variety of features and capabilities. One of the most popular features of Excel is the ability to create and run VBA macros.

A macro is a small scripts written in the VBA (Visual Basic for Applications) programming language that can be run in your desktop Excel app.

Macros can save you a lot of time and energy when working in Excel. They can be used to automate tedious tasks and save you hours of work each week.

But in order to leverage this time saving tool, you will need to know how to run your VBA macros.

Follow this guide and you’ll be able to start running macros like a pro in no time!

Run VBA Macro from the Developer Tab

The most common method for running a macro is from the Developer tab in the Excel ribbon.

This tab is hidden by default, so you will need to enable the Developer tab in your desktop Excel app first.

Follow these steps to run a VBA macro from the Developer tab.

  1. Go to the Developer tab.
  2. Press the Macros command in the Code section.

This will open the Macro menu which lists all the macros available to run.

  1. Select the macro which you want to run.
  2. Press the Run button.

That’s it! Your chosen macro code will now execute!

💡 Tip: Use the Macros in dropdown option to select the location of macros to run. You can select a specific workbook, All Open Workbooks, or This Workbook.

Run VBA from the View Tab

The Macro command is also available in the View tab.

Go to the View tab and press the Macros button to launch the Macros menu.

This opens the same Macro dialog box as before and you can select the macro and press the Run button.

Run VBA Macro from Macro Menu with a Keyboard Shortcut

There is an easier way to open the Macro menu! There’s no need to use the Developer or View tab since there is a dedicated keyboard shortcut to open the Macro menu.

You can use the Alt + F8 keyboard shortcut to open the Macro menu.

Run VBA Macro from a Keyboard Shortcut

You can entirely bypass the Macro dialog box by assigning a keyboard shortcut to your desired Macro.

If this is a macro that you want to use a lot, then assigning a shortcut is a good way to avoid the many clicks needed when running a macro through the Macro menu.

Follow these steps to assign your macro a dedicated keyboard shortcut.

  1. Open the Macro menu.
  2. Select the macro to which you want to assign a keyboard shortcut.
  3. Press the Options button.

This will open up the Macro Options menu where you can add a description for the macro and assign a keyboard shortcut.

  1. Add a character into the Shortcut key input box.
  2. Press the OK button in the Macro Options menu.
  3. Press the Cancel button in the Macros menu.

⚠️ Warning: This chosen shortcut key will override any existing keyboard shortcut, so you should avoid using keys taken by commonly used shortcuts such as copy, paste etc.

A lot of Ctrl and single key combinations are already taken with commonly used commands, so you might want to create a Ctrl + Shift shortcut instead.

💡 Tip: Hold the Shift key while entering a key in the Shortcut key input to create a Ctrl + Shift shortcut.

Run VBA Macro from a Form Control Button

If other people are using your spreadsheet solution, they might not realise they can run your macros to help complete their work.

This is where a button is the preferred choice to run a macro. It makes the act of running your macro easy and obvious!

You can create a Form Control Button to run the macro when you click the button. This can be placed anywhere in the spreadsheet since is floats over top of the grid. This means it won’t interfere with the rest of your data or formulas.

Follow these steps to insert a Form Control Button and assign a macro to it.

  1. Go to the Developer tab.
  2. Click on the Insert command.
  3. Choose the Button option found in the Form Controls section.

This will not actually insert a button yet. You will notice your cursor has now turned into a small black plus sign. This will allow you to draw a button in your sheet.

  1. Left click and drag anywhere in the sheet.

When you release the click and drag action, the Assign Macro menu will immediately pop up and you will be able to assign your macro to the button.

  1. Select your macro.
  2. Press the OK button.

Now you have a button in your sheet which will run your select VBA macro when clicked.

💡 Tip: Righ click on the button and select Edit Text to change the text displayed on the button.

Run VBA Macro from any Shape, Icon, or Image

Form Control buttons are pretty ugly and outdated. They also don’t have many options to customize the look, but thankfully they aren’t the only way to make a button to run your macros!

There are some much more stylish options like using an image, shape, or icon as a button to run your macros.

Follow these steps to assign a macro to any object such as an image, shape, or icon.

  1. Right click on the object.
  2. Select Assign Macro option from the menu.
  3. Select the macro from the Assign Macro menu.
  4. Press the OK button.

Now when you click on the shape, image, or icon it will execute the macro code!

Run VBA Macro from a Quick Access Toolbar Command

Another option is to add your most frequently used macros to the Quick Access Toolbar.

The Quick Access Toolbar is a customizable set of commands that are always visible so you can easily use them at any time. You can even add a macro so it can be run with a click.

Follow these steps to add a macro to your Quick Access Toolbar.

  1. Right-click anywhere on the Quick Access Toolbar.
  2. Select the Customize Quick Access Toolbar option from the menu.

This will open the Excel Options menu on the Quick Access Toolbar section.

  1. Select Macros from the Choose commands from dropdown.
  2. Select the macro you want to add to your Quick Access Toolbar.
  3. Press the Add button.
  4. Press the OK button.

When you press the Add button you will see the selected macro gets added to your list of commands. You can use the Up and Down arrow buttons to adjust the order this macro will appear in your commands.

💡 Tip: Press the Modify button in the Excel Options to change the icon and label of the macro that will appear in your Quick Access Toolbar!

Now you should have a new icon available in the Quick Access Toolbar. Click on this to run your select macro.

💡 Tip: An easy way to use the commands in your Quick Access Toolbar is with the Alt hotkey shortcuts. In this example, the command is in the 8th position starting from the undo command, so you can press the Alt + 8 to run the macro.

Run VBA Macro from a Custom Ribbon Command

If you have an entire repertoire of macros and you’re running out of room in the Quick Access Toolbar, then adding a custom ribbon tab to organize your macros could be the ideal solution.

Excel allows you to add your own custom ribbons and fill them with your favorite macros as well as any other commands you frequently use.

Follow these steps to add a macro to the Excel ribbon.

  1. Right click anywhere on the Excel ribbon.
  2. Select the Customize the Ribbon option from the menu.

This will open the Excel Options menu on the Customize Ribbon section.

  1. Press the New Tab button to create your new ribbon tab.

  1. Press the Rename button to give your tab a name.

Each tab will need at least one group, and this is automatically created when you create a new tab. You can also rename the group. Both of these names will be displayed in your Excel ribbon.

Now you will be able to add a macro into the new tab and group.

  1. Select the Macros option from the Choose commands from dripdown.
  2. Select the macro which you want to add into the ribbon.
  3. Press the Add button.
  4. Press the OK button.

💡 Tip: Select the tab and use the Up or Down arrow buttons to adjust the position of the new tab in your ribbon.

You now have a new custom tab that can hold all your most frequently used macros. 😃

Run VBA Macro from Visual Basic Editor Run Menu

The visual basic editor (VBE) is the environment where you write VBA code, so it makes sense that you should be able to also run your code from it.

A lot of people like to test their code as they develop their solutions and this means frequently running your macros from the the editor. Your current code can always be run from the Run menu in the VBE.

Follow these steps to run your macro from the Run menu in the visual basic editor.

  1. Select the macro you want to run.

You can select the macro by either placing the cursor in the code or selecting the macro name from the dropdown menu in the top right.

  1. Go to the Run menu.
  2. Select the Run Sub/UserForm option from the menu.

This will run your selected macro!

Run VBA Macro from Visual Basic Editor Toolbar

The visual basic editor comes with a toolbar for easy access to the most frequently used commands.

Follow these steps to run your macro from the toolbar.

  1. Select the macro you want to run.
  2. Press the Play button in the toolbar.

This will run your selected macro!

📝 Note: If you don’t see this toolbar you might need to enable it. Go to the View menu then Toolbars and check the Standard option.

Run VBA Macro from Visual Basic Editor Keyboard Shortcut

There are a lot of very useful keyboard shortcuts for using the visual basic editor.

Running a macro from the VBE is a very common task, so it’s no surprise there is also a keyboard shortcut available for this.

Press the F5 key while in the VBE and the currently selected macro will run!

Run VBA Macro from Another Macro

You can easily run a macro from a macro in Excel.

This is a good practice when it comes to programming. Creating smaller procedures and then reusing them within your main macro can be more efficient to run and easier to maintain the code.

Sub ExampleCode()
    MsgBox ("Hello world!")
End Sub

Sub MainCode()
    Call ExampleCode
    MsgBox ("Goodbye!")
End Sub

You can easily run any macro from within a macro using a single line of code. The above example will run the ExampleCode macro from the MainCode macro.

Run VBA Macro from a Worksheet Event

Did you know you can automatically run a macro?

You can automatically run a macro based on events that happen in your Excel worksheet!

For example, you can have a macro run anytime someone changes a value in the sheet.

Follow these steps to create a worksheet event-driven macro.

  1. Select the Sheet in which you want to trigger the macro. All your workbook sheets will be listed in the Microsoft Excel Object folder of the VBE Projects.
  2. Select the Worksheet options from the dropdown menu.
  3. Select the event type that should trigger your macro.
Private Sub Worksheet_Change(ByVal Target As Range)

End Sub

When you select the type of event, it will insert a bit of code into the editor. For example, the Change event trigger will insert the above code.

  1. Place any code you want to run when the event occurs inside the generated code.

If you have an existing macro that you want to run, you can call it using a Call YourMacroName single line of code.

This macro will now run anytime you make changes in any cell within Sheet1.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then Call ExampleCode
End Sub

You might want to limit the macro to only running when the change event occurs in a particular cell or range. This is possible by setting conditions for the Target in your code.

In the above example, the ExampleCode macro will only be called when changes are made to cell A1.

Run VBA Macro from a Hyperlink

Did you know you can trigger your macros to run when you click on a hyperlink in Excel?

This is particular worksheet event method is worth its own mention!

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Address = "B2" Then Call ExampleCode
End Sub

The above code will run every time you click the hyperlink in cell B2 in Sheet2.

The code will execute and then take you to the hyperlinked address!

Run VBA Macro from a Workbook Event

There is also the possibility to automatically run a macro based on workbook events such as when you open or close the file.

This is a great option to make sure a task is performed before you do anything else in your workbook.

Follow these steps to run a macro automatically when you open your Excel file.

  1. Select ThisWorkbook found in the Microsoft Excel Object folder of the VBE Projects.
  2. Select Workbook from the dropdown menu.
  3. Select Open from the event type dropdown menu.
Private Sub Workbook_Open()

End Sub

This will insert the above code into the code editor. You can then add any code inside which you want run when you open the file. You can also call any macro here with the Call YourMacroName single line of code.

Conclusions

VBA macros can be used to automate your tasks in Excel, so it’s important you know how to run them.

There are many methods to run your desired macros depending on your situation.

You can use the Excel ribbon, a keyboard shortcut, or a customized quick access command when you want to manually run a macro. Also, you can attach your macros to run from buttons in the workbook to make it more user friendly.

You might need to occasionally run your VBA macros while developing your solutions to test them. This can be done several ways in the visual basic editor.

You can even trigger your macros based on certain worksheet or workbook events for the ultimate in automation.

Are you using macros in Excel. Did you know all these methods to run your macros? Do you know any others? Let me know in the comments below!

About the Author

John MacDougall

John is a Microsoft MVP and qualified actuary with over 15 years of experience. He has worked in a variety of industries, including insurance, ad tech, and most recently Power Platform consulting. He is a keen problem solver and has a passion for using technology to make businesses more efficient.

Понравилась статья? Поделить с друзьями:
  • Macro on opening excel
  • Macro in word template
  • Macro in excel with button
  • Macro in excel not working
  • Macro in excel file