Thursday, 16 April 2020

How to Record your First Macro (VBA).


Namaste 🙏 my dear friends.

We are back again with another new and interesting topic related to Macro.

In our last blog we discussed about meaning and basics of the Real Power of Excel i.e. Macro.


Ø    There are basically two ways through which we can create a Macro.
  §  First, we can write the codes directly in VBA editor.
  §  Second, we can record our steps and excel itself will convert those steps into code and record it in VBA.


In this blog we will learn the Second way i.e. How to Record a Macro using record function in excel.  

So not wasting much time Let’s Begin. 

Suppose we need to change the formatting of a particular cell, let’s say, Background Colour as red and Font Colour as White. We will create the macro for same.

Ø     For this we will execute two steps: -
1)      Select and change background colour as Red.
2)      Select and change font colour and then as White.

Ø     We will record the above steps in Macro through following steps: -

1)      Go to Developer Tab. (If the developer tab is not showing in your excel you can click here.)



2)      Click on Record Macro button which will be at left side of the screen.



3)      After clicking Record Macro button, Record Macro Window will be open with following options: -


         i)      Macro Name: – Enter the Name of the Macro, it will not accept space so we will name it as ‘Format_Macro’.
        ii)    Shortcut Key (Optional): - This will help to execute the macro by pressing the combination of keys. By default, ‘Ctrl +’ combination is given. We can also use shift along with Ctrl, so let’s set the combination as ‘Ctrl+Shift+F’ by pressing ‘Shift’ and ‘F’ together.
       iii)   Store Macro in: - This ask us where to store the macro, whether in this workbook or in all open workbook as well.
     iv)   Description (Optional): - Many times there are lot of Macros in single workbook, so by writing the description of macro we can easily identify the function of the particular macro.



4)      Press OK after filling the above details.
5)   We can see at the bottom left there is a square icon which indicates that macro has started recording.


6)      Execute the action or steps: -
a)       We will select the cell “A1”.
b)      Change the Background Colour to Red. (Home Tab -> Fill Color-> Red)
c)       Change the Font Colour to White. (Home Tab -> Font Color -> White)

Change Background and Font Colour

7)      Stop Recording: -
       a)       After successfully recording the steps we will stop the recording of Macro.
       b)      Go to Developer Tab.


       c)       Now press “Stop Recording” option.


Now we'll check whether is it working fine or not?
So, we will now select Cell “A2” and press the combination button we set for our macro i.e. “Ctrl+Shift+F”.





Eureka 😃, we did it. Its time to celebrate as we have successfully recorded our first Macro.
As we mentioned above, through Record Macro Option, excel itself converts our steps into VBA codes. 
Now, let’s find out the code of our actions or steps: -
1)      Go to Developer Tab.
2)      Click on Macros option.



3)      It will open the Macro Window.
4)      Select the macro i.e. ‘Format_Macro’ and press Edit.
Edit Button in Macro

5)      By pressing edit it will Open VBA Editor.
6)     In VBA Editor on the right-side new window will open which have VBA codes written in it.
7)     These Codes are written under Modules in this case “Module1” which can be seen on left-panel.




I hope we have learned how to record a macro using record function and how to view the codes of same.

You can watch the Video of above Topic below:-


So, this is the end of todays topic, we will come soon with another interesting topic related to Macros until then Namaste🙏😊.

No comments:

Post a Comment