What are Macros?
A macro in computer science is a rule or pattern that specifies how a certain input sequence should be mapped to a replacement output sequence according to a defined procedure. (Wikipedia Definition)
In simple words, a macro in excel is a set of instructions to perform on a data set to manipulate or process the data and produce a desirable output which when done by in-built functions would not be possible.
A macro is a custom instruction to your excel sheet which can do repetitive tasks and you can call an excel macro to perform the same operation multiple times to save time. It is a code snippet (much like programming) that runs in Excel environment.
Although, you need to have a basic knowledge of VBA programming.
What is VBA?
VBA stands for Visual Basic for Applications.
If you are a programmer, you may be familiar with visual basic programming. VBA is an environment provided by Microsoft to run Visual Basic codes within Excel to record your steps as you perform routine tasks.
But you do not need to be a programmer to do basic macros for excel as there are various tools within excel that automatically generates the source code for you.
Macro Basics
Well a macro is a developer tool present in Excel which needs to be turned on. We will give a tutorial to do the same in the next section. As macros as disable by default in Excel, you may only turn these features on when you know what are you doing with it.
Some hackers may take advantage of this and can run some macro script to perform malicious actions to steal or corrupt your data. Be aware about these concerns and check with your firewall once to ensure everything’s going smooth.
How to Create a Macro?
To enable macros, we need to turn on the developer tab on the ribbon.
Follow these steps to activate developer options in Excel:
- Right Click anywhere in the Ribbon
- Click on Customise the Ribbon
- Check the Developer checkbox within the Main Tabs selection pane.
- Click on OK
Now you’ve enabled the Developer tab within your Excel software.
Our next step would be to actually create a simple macro and implement it with an example
Creating a Basic Macro:
First, we will place a command button to our sheet.
- Go to the Developer Tab and Click on the Insert Button.
- Select the Command Button (as shown in the image above)
- Your cursor changes to + sign, click anywhere on the sheet to insert the Command Button now.
We now have the Command Button on our sheet. You can observe the Formula bar to see the actual formula used to Embed this button in the excel sheet.
Coding your Macro
Now we will code our macro with a very simple and understandable code so you can get the hang of it.
We will try to populate the cell range E1:E5 with value equal to 100.
Steps:
- Right Click on the Command Button.
- Click on View Code option
- This will open the Code editor i.e. the Microsoft Visual Basic for Application (VBA) editor.
- Write the below code within the space given to edit the code snippet.
- Then Click on the run Button
Voila! We have now populated our cell range E1:E5 with 100 as the Value