Prerequisite
We will start learning by considering that user has Microsoft Excel 2007 onward installed on his machine.
VBA is a scripting approach by which user can automate day to day jobs easily within Excel environment.
Programming Environment
Just don’t worry, you don’t need to install or buy any other software to program your VBA jobs. VBA programming environment is available within Excel only. Note, VBA is a scripting language and supports automation, however, if you are good in programming then you can implement large size application using VBA but truth is VBA is not build for building professional or enterprise level applications.
Audience
Who can learn VBA? User having knowledge of Excel and little programming skill makes it great.
Excel VBA Editor
VBA Editor window let you write/edit/run/edit/delete VBA code.
There are two way to view VBA Editor Window in Excel
- View Editor Window using Excel Short Cut Keys
- View Editor Window using Ribbon
First Approach : Press ALT + F11 keys from keyboard to view VBA editor window.
Second Approach : You can view VBA editor with the help of Developer Tab. if Developer tab is not visible please follow the steps to include it in Excel Ribbons.
Steps:
Click File Menu>>Choose Options>>Select Customize Ribbon as shown below:
Check Developer under Main Tabs List which is located at right side and press OK. Once you are done you will notice Developer tab is available in Excel Ribbons as shown below
In above figure very first option click on Visual Basic button and you will be shown following window as shown below:
Start Scripting and Show “Hello VBA”
To write your first script follow the instructions as below:
Click on the Insert command on Developer tab as shown below and select Command Button from underlying controls. Insert command has two sections which we will understand later sessions.
On selecting command button, user needs to select the drawing area on the sheet where he/she wants to have button and release the mouse. Upon releasing mouse, Excel Assign Macro popup comes as shown below:
Lets move forward and cancel this dialog for now as we don’t have any macro which can assign to the button. And now you see the button has been placed on the sheet as shown below:
Now rename button by double clicking it or right click and select edit text command from popup menu and name it as My Test as shown below:
Now create your first macro to make your button functional.
Click on VBA Editor button or press ALT + F11 key to bring Editor.
Now follow the steps:
Under VBA Editor click on Insert menu and select Module (we will understand each command in later sections) as shown below:
Now again click on Insert menu and select first option Procedure. Upon selecting Procedure command it brings you a popup as shown below:
Just enter the name anything excel numbers or special characters as VBA does not support them as name of a procedure and leave rest default (we will learn each in detail in coming sections) hit enter or click OK button.
Upon hitting enter, VBA creates a procedure which you can use to write your code. Now you are wondering what is a procedure?. Aha!!! no worries its a building block, within you write your code.
In above screenshot you can see where written “Your code goes here”. That’s the are where we need to write our fist code. So, no more wait, lets jump over writing the code:
So, here we go, I have written a simple line of code which says Hello VBA. Now go back to the Excel sheet where we have inserted our button My Test. To reach back to Excel, Point mouse over and select Sheet or Click on Excel icon as shown in below image.
Now you can see your button (My Test), right click on the button and select Assign Macro command from popup menu as shown below:
Again Excel brings Assign Macro dialog as previous but this time it will have our procedure listed under it as shown below:
Just click or highlight our procedure name by clicking on it as shown above and say OK. The assign macro popup goes off. Now the real magic starts, click on the sheet any where and try bringing mouse over the button (My Test), wow!!! you will realize the pointer turns in Hand icon it means you are ready with your code to be executed. Now simply click on the button and result will be like as shown below:
That’s it. Your first VBA test is delivered. We will learn type of Modules in VBA.