2. Add Multiple Tasks to a Macro

Subtitles Enabled


A Macro's real power is combining multiple tasks into a single command. In this lesson, we'll learn how to append additional tasks to the original Macro.

Lesson Notes

What happens when we record a Macro

- When you record a Macro, Excel interprets your actions as a block of code and saves it
- To see the code, you need to open the Visual Basic Editor
- When saving a Macro, you use the currently open workbook or the Personal.xlsb file
- Use the Personal.xlsb file if you want to use the Macro in multiple workbooks.

Keyboard shortcuts

ALT + F8: Open Macros dialogue box
- You can also useALT + L, P, M
ALT + F11: Open / Close VBA editor
- You can also useALT + Q
ALT + L , R: Stop / Start recording Macro
ALT + H , H: Change background color
ALT + H , F, C: Change font color
ALT + H , B, S: Add outside borders to a cell
CTRL + SHIFT + %: Convert cell format to percentage


In the previous lesson we recorded a very simple macro that changed the background color of our cell to blue, color of the text blue, and bolded the text.

In this lesson, we're going to extend the functionality of our macro to convert the cell format to percentage and put a border around the cell.

Before we do that however, let's understand exactly what happened when we recorded our first macro.

In the Developer Tab we can click on the Macros button or press the shortcut Alt + F8.

And this lists the macros that are available in the current Excel sheet.

As expected, we've one macro called percentInput which we created in the previous lesson that's stored in the current Excel file.

As a result, it's only available in this file.

If you'd like to make the macro available across all files, then you should store the macro in a file called Personal.XLSB, which is a hidden program that Microsoft runs when Excel is opened.

On the right hand side let's go to the Edit button.

And when I hit Edit, a few lines of VBA code appear on the screen in a window that's called the VBA Editor.

What has happened is the following.

As we record our macro on the screen, Excel is working underneath to convert our recording into VBA code.

This includes the name of the macro at the top, a number of green lines, which are comments that include our macro name, description and shortcut and underneath we have the code generated by our recording.

Here it tells us that a cell has been selected.

The following changes have been made to the background color.

The following changes to the text color and the text has lastly been bolded.

For simple macros such as this example, there's normally no need to look at the code generated, however for more complex Macros, the recording doesn't always work immediately and some editing to the code may be required.

Our current macro has three simple tasks included in it.

To utilize the real power of macros we can combine even more tasks into one command.

Let's say I also want to add a border to the selected cell and convert the cell to the percentage format.

So let's close the VBA editor with Alt + F11 and return to our Excel sheet.

I'll navigate to a new cell, record a new Macro, call this macro finalInput.

I'll give it a new shortcut, say, Shift + Y.

I'll store it in This Workbook and in the description I'll add, Final percent input format and press OK.

My first task as before would be to go to the Home tab and change the background color. So I'll press H and select the required color.

Then I'll change the font color to the same blue we had before and I'll bold it with Ctrl + B.

I'll now add a border, with Alt + H, B and then S for Outside Border.

And lastly, I'll change it to the percentage format with Ctrl + Shift + 5.

I'll then return to the Developer tab and stop the macro.

Let's now test this macro to see if it's working correctly.

And as you can see, it adds a border, changes the text and the background to the correct colors and leaves my input in the correct format.

Before wrapping up this lesson, let's go back and take a quick look at the code just generated by this new macro.

So I'll press Alt + F11 to open up the VBA editor.

As you can see a new macro has been created called finalInput with the description and the keyboard shortcut included.

As before, we have a section of code for changing the background color and changing the font and bolding the font.

We now also have a new section related to the borders and as you can see Excel has a width and block of code for each of the cell's four borders.

In the last line, the number format has also been changed to percentage.

Even though we haven�t covered any VBA coding yet, it's pretty easy to understand how some of these commands work.

Let's say for example, we'd like to add on a decimal point to our percentage format.

I'll simply change this value to 0.0 and now returning to my Excel sheet, and using the shortcut Ctrl + Shift + Y, this should give me 20.0% and it does.

And what's more, I can apply this macro to the previous cells as well, Ctrl + Shift + Y.

So now, without even knowing how to program we've already begun to edit VBA code in Excel. Congratulations!