
Testing the macro
It's all very well to have recorded a macro, but how do we know whether it's working or not? There is only one way, and that is to test it. Sure, you can have a look at the VBA code in the VBA Editor, but unless you are a seasoned coder, it will be virtually impossible to find coding errors. Best then to do a practical test.
Getting ready
Before moving on, make sure that you have recorded the macro as per the instructions in the previous recipe. We will be using that code in this recipe.
Since we've previously used Sheet1 to record the macro, we will have to test it on Sheet2. Testing on Sheet1 will have no effect because the cells have already been formatted. Sheet2 is still in its original form, meaning we will immediately notice any changes.
How to do it…
Let's proceed with the following steps:
- Make sure that 02_RecordMacro.xlsx is still open, but this time on Sheet2. To test how effective the Relative Reference setting is, select any cell other than cell A1.
- Let's use the Ctrl + Shift + F keyboard shortcut and see what happens:
Figure 1.13 – The formatted sheet after running the macro
The content is now formatted in exactly the same way as in Sheet 1. Isn't that much faster than formatting each area manually?
- Activate Sheet3, because there is yet another way to run a macro. Select any cell on the sheet.
- This time, select the Developer tab and click on the Macros icon in the Code group:
Figure 1.14 – The Macro dialog box
- The only available macro is the one we recorded, as you can see. To the right, there are several buttons. Click the top one, Run.
- As expected, Sheet3 is now also formatted in the same way as Sheet1 and Sheet2.
How it works…
When we first recorded the macro, the Record Macro dialog box gave us the option to select a keyboard shortcut. Clearly, our instructions there have worked.
A slightly longer, yet still effective, way is to run a macro from the Macro dialog box. Since there is only one macro, we can only execute that one. However, if there were more, we could have selected any one before clicking Run.
There's more…
It is good practice to always test a macro, instead of assuming that it will work. It will be exceptional for a recorded macro to fail, but when we get to more advanced subroutines – especially when you start typing your code manually – you should never assume that your coding will be flawless.
So, when everything is working the way it should, it may be a good idea to investigate the actual VBA coding.