In our previous blog, we had learned to create a macro by recording it. In this blog, we would learn multiple ways to run a macro in excel. There are four ways that you can use to run and execute a macro in Excel.
Before this, you need to have a macro ready in your workbook. You can create a macro by either recording a new macro or by writing a VBA code in Visual Basic Editor (VB Editor).
Firstly, let us for this blog, write a small VB code that enters the text ‘Excel Unlocked’ in the cell A1. To do this, follow the under mentioned steps.
Press Alt+F11 on your keyboard to open the VB Editor window.
Right-click on the Sheet1, then select the option ‘Insert’ and click ‘Module’.
The excel would insert a new module named ‘Module 1’ and the code window.
Now, copy the below VBA code (This VBA code would insert the text ‘Excel Unlocked’ in cell A1) and then paste it in the ‘Code Window’.
Sub RunMacro()
ActiveSheet.Range(“A1”).Value = “Excel Unlocked”
End Sub
Here in the first line of this code, the string ‘RunMacro’ represents the name of the Macro.
Now Close the VB Editor Window by clicking on the “X” button on its top-right corner.
Methods To Run A Macro in Excel
There are basically four methods that you can use to run a macro in Excel. These are:
- Using the ‘Run Macro’ Dialog Box
- VB Editor Window
- By Assigning Macro to Shape
- By Assigning Macro to Button
Let us now learn each of these methods one by one.
Run Macro Dialog Box in Excel
This is the basic method to run a macro in Excel. In order to run a macro using this method, you need to first activate and enable the ‘Developer’ tab in Excel. Please go through our previous blog on ‘Start Automation – Record a Macro in Excel’ where you would learn how to activate the ‘Developer’ tab in Excel.
It looks like this:
Now, when you have this tab in your workbook, follow the below steps to run the macro.
Under the ‘Code’ group of the tab ‘Developer’, click on the button ‘Macros’.
In the ‘Macro’ dialog box that appears on your screen, you would find the name of your macro ‘RunMacro’. Just select this and click on the button ‘Run’ as shown in the screenshot below:
As a result, you would notice that the excel executes the code behind this macro and writes the word ‘Excel Unlocked’ in cell A1.
Using VBA Editor Window
Another method to run a macro in excel is by using the run button on the VBA editor window. Follow the below steps to achieve the same.
Also Read: Start Automation – Record A Macro in Excel
Again, we need to have the ‘Developer’ tab enabled on our screen.
Under the ‘Developer’ tab option, click on the button named ‘Visual Basic’ as highlighted in the screenshot below:
The ‘VB Editor’ window will appear on your screen with ‘Module 1’ as a selected node and your code would also be visible over there.
Just above the code window, you would notice the ‘Run Sub/UserForm’ button as highlighted in the screenshot below:
Click on this button or use the keyboard shortcut F5 (need to click on Run button), and you would notice that the code executes and writes the word ‘Excel Unlocked’ in the cell A1.
Run Macro By Clicking on the Shape
We can even insert a shape in the worksheet and just by clicking on this shape, we can execute the macro code.
Follow the below steps:
Insert the shape of your choice using ‘Insert’ Tab > ‘Illustration’ Group > Click on the ‘Shapes’ button and click on the shapes of your choice.
Click and drag on the part of the worksheet where you want to insert the selected shape.
Now right-click on the inserted shape and click the option ‘Assign Macro’.
The ‘Assign Macro’ dialog box would appear on your screen. You would notice that your macro is available in the list. Select it (RunMacro) and click on OK.
The macro now is assigned to the shape.
Now take your cursor on the shape, and you would see that the mouse pointer changes its shape to a hand-like symbol.
Now, click on the shape and you would see that the macro gets executed and inserts the word ‘Excel Unlocked’ in cell A1.
Running Macro Using A Click Button
This method works in a similar way as the above method. Instead of inserting the shape, we would use the button to execute the macro.
The difference between a button and a shape is that you can format a shape, but you cannot format the button. However, you can edit the button text.
Follow the below steps:
Firstly, we need to insert a button. For that, we need to have the Developer tab enabled/activated.
Then, go to the ‘Developer’ Tab > ‘Control’ group > ‘Insert’ Option > Click on the ‘Button’ option as highlighted in the screenshot below:
As a result, the mouse pointer would change to a plus symbol. Click on the location where you want to insert the button.
As soon as you click, the ‘Assign Macro’ dialog box would appear on your screen. Select the macro name (RunMacro) and click on the ‘OK’ button.
As a result, the excel inserts the button (with default name as ‘Button 1’). You can change the name by right-clicking on the border of the button and select the option ‘Edit Text’.
Once you change the name of your button, press the ‘Escape’ (Esc) button on your keyboard to deselect the button.
Now, take your mouse cursor on the button and you would notice that the excel changes the mouse cursor to a hand-like symbol.
Finally, click on this button and you would notice that the excel executes the macro and inserts the text ‘Excel Unlocked’ in cell A1 (based on the VBA Code).
By this, we have complete with all the methods using which you can run a macro in Excel.
Share your views and comments in the comment section below.