Running Macros Workout
- 05:10
Demonstration of a number of methods to execute VBA code or run a macro.
Transcript
When it comes to executing VBA code or running a macro, there are a number of options available.
A macro can be run by the macros button in the developer ribbon to view the macros ALT F 8 keyboard shortcuts.
It can be run by an assigned shortcut key, and we can create a button on the Excel worksheet that will run the macro.
Let's take a look at each of these methods of running a macro.
Let's take a look at some of the options that we have available to us to run our macros.
First of all, we need to record a macro and the workout requires us to record a macro to enter our name into cell B 8.
In order to record a macro developer record macro, let's call this one enter name one.
I won't assign a keyboard shortcut yet and will store the macro in this workbook.
Once I click okay, the macro recorder will record every cell that I select and it will record all of the information that I enter into those cells.
Select cell B 8.
Enter my name, and then press enter.
We can stop the macro recorder developer and stop recording.
Let's look at some methods that we have available to us to run that macro.
To ensure that the macro is running we'll delete the contents of cell B 8.
Select another cell, and then we can see if the macro is running the first option.
Develop a tab, view macros. There's our macro.
Enter, enter name one, and press run, and we can see that my name is entered into cell B 8.
This is a perfectly good method to run a macro, but potentially not particularly user friendly.
Let's take a look at another option.
We can assign a keyboard shortcut.
Go to developer view our list of macros.
Enter to name one, our macro is selected.
Clicking on the options button allows us to assign a shortcut key.
I'm going to assign the shortcut, key control, and W entering W and pressing.
Okay, and then closing the window will mean that in this file, when I press Ctrl and W, my macro enter, enter name one will execute.
Let's test this out. Clearing out the cell contents of cell B 8 and choosing another cell on our spreadsheet control and W executes the macro and enter name one enters my name into cell B 8.
This is a little more efficient.
The downside here is that another user may not be aware of the shortcut key and potentially override an existing shortcut.
The final option that we'll look at is to use a form control button.
In order to do that, let's go to the developer menu and we insert the button via the insert controls button.
When we expand that menu, we can see that the form control button is here at the top left.
Let's click on the form control button.
We can see the cross air Hold down the left mouse key and draw our button.
Once that button is drawn, a window is then presented to us.
That requires us to assign a macro to the button.
We're going to assign the macro enter, name one and press okay.
I can also change the text on button one by clicking into the button, deleting button one and entering new text.
Let's click out of the button and test this hovering over the button.
I can see the pointer, click on the button.
The macro executes and my name is entered into cell B 8.
This is a great option to provide our users with additional interactivity.