VBA Tutorial Beginners
VBA Tutorial Beginners
VBA Tutorial Beginners
COM
http://www.learnexcelmacro.acom
Vishwamitra Mishra
9/11/2012
This is free E-Book for Excel Macro Beginners. Here you will learn very basic things about Excel Macro.
Contents
http://www.learnexcelmacro.com Page 2
About this E-Book Is it useful to you or Not?
Dear Friend,
First of all I would like to thank you for downloading this E-Book. This E-Book is basically for
the people who want to start coding in Excel VBA and make their day-to-day work easier by
writing small codes. If the answer of any of the question below is NO, then this is the right
book for you.
So, did you find any of the above questions which have your answer as NO?
If not, sorry this book may not be useful to you, still you can go through it and give me some
feedback and areas to be improved etc.
If yes, go ahead you have chosen the right E-Book. This book will not make you awesome in
Excel VBA but it will definitely enable to become awesome in excel VBA. Its like the first
step towards making you excellent in Excel VBA. As you know that the foundation of any
construction is more important than the whole construction. If the foundation is strong, your
construction is going to be very much strong.
I will not waste any more time of yours in this question answer session and lets begin
http://www.learnexcelmacro.com Page 3
Macro is very popular software available with the most of the windows applications. Many of
the windows applications provide inbuilt Macro programming. For example: Excel, Word etc.
In this tutorial, I will be taking example of MS Excel and will be exploring each and every
possible technique to do the things in a very easy manner.
In MS Excel, we have both recording and writing our own macros based on our need. Later in
this Book, you will see both the techniques How to write macro in Excel and How to record
macro in Excel.
Before you jump in to writing VBA code lets see few important points which will be required
while writing or recording VBA code.
This is the Tab where all the Excel VBA related options, Controls, settings etc. will be visible. In Excel
2007 onwards all the VBA related stuffs are grouped under Developer Tab. By default Developer Tab
is not visible in the ribbon as shown in below Image
http://www.learnexcelmacro.com
So before you start Excel VBA, you need to know how to make Developer Tab visible in the Excel
Ribbon.
http://www.learnexcelmacro.com Page 4
Steps to Add Developer Tab in your Excel Ribbon:
5. Click Ok
http://www.learnexcelmacro.com Page 5
Now you can see the Developer Tab visible in your Excel Workbook. Now it
will be always visible until you go back and uncheck the check box which
you have checked it before to display.
You can also watch video tutorial for the same thing on YouTube as well.
You can also refer the tutorial for the beginners on my site:
http://www.learnexcelmacro.com/2011/10/add-developer-tab-in-excel-2010-and-2007/
1. Open your workbook for which you want to write VBA code
2. Press Alt + F11 from your keyboard
3. VBA Code window will appear as shown below
http://www.learnexcelmacro.com Page 6
1 2
3 4
An Overview of VBA Code Window
http://www.learnexcelmacro.com Page 7
iii) Section 3 (Property Window): This is a property window. While doing VBA
programming at any point of time whenever you
select any Object or Sheet, automatically this
window will start showing you the properties of
that selected object. In this case since we have
selected the Sheet1, so it is displaying the
properties of the Sheet1. In this property window,
the first field (Name) is the name of the Object
which used during the coding. Each Object can be
referred by this name of that Object as this is the
unique name. This is not used for displaying
purpose. For example in this Case, if we change
the first field (Name) to VISH then also the
display name of that Sheet will remain same as
Sheet1. Shown in below image:
http://www.learnexcelmacro.com Page 8
If you have to change the Display Name of the Sheet then change the Name field value of the
property window. Refer the below Image
Note: Display Name can accept Spaces in between but the Object can not accept any space in
between. If you provide any Name in between then it will throw an Error.
iv) Section 4 (Watches Window for Debugging): This section is used only while
debugging a code. When you are running your code in debugging mode that time you
can add as many watches as you can for different variables, Objects etc. to see their
values live. We will discuss more about this section later.
http://www.learnexcelmacro.com Page 9
So this was an overview how to open the VBA code in Excel. We have also discussed about all the 4
main section of this VBA code window.
Now we will learn how to write the First Macro in Excel VBA.
In VBA all the code must always be written between Sub and End Sub.
As the name suggests, this event triggers as and when the selection in the Sheet1 (Changed Name
VISH) Changes. It means that whatever VBA statements will be written between this Event, it will be
automatically executed as soon as the selection on the Sheet changes.
Maintaining the tradition of first programme of any Programming Language, we will also display a
message Hello World on every selection change in Sheet1.
http://www.learnexcelmacro.com Page 10
The Syntax to display a simple message box is as follows: MsgBox Your Message Here
You can be lazy enough here to type letter M and B in capital letter and type it as msgbox. See
what excel does Since msgbox is a known keyword to the Excel compiler, it will automatically make
the msgbox in a proper case. It means msgbox will turn to MsgBox
End Sub
Save your Workbook. Did you realize that you have written your first program? Congratulations!!
Now its time to see your macro runningvery happy now? Ok lets see it running
Simple Now in your workbook as soon as you select any Cell one message box will appear with
message as Hello World as shown in the above image.
http://www.learnexcelmacro.com Page 11
Now we will see how to debug a Program or how to run a program in debug mode.
MsgBox message
End Sub
Note: I have declared a variable named message. In this variable I am storing a message Hello
World. And then finally I am displaying the value stored in that variable in a message box. Now lets
start to run the program in debug mode.
http://www.learnexcelmacro.com Page 12
5. Once the break point is put, now do the operation because of which that event will trigger.
Like in this case, this event will trigger once we change the selection in the Excel Sheet.
6. After changing the selection the Event will trigger and Code will automatically stop at the
point where you have put your break point as shown in the below image. The break point line
would be highlighted now in Yellow Colour as the execution is stoped at that point. Now you
can go line by line and execute the statements thereafter.
The above YELLOW highlighted statement is not yet executed but before that all the statement are
executed.
http://www.learnexcelmacro.com Page 13
9. Now we have added a watch for the variable message. To execute the statement Press F8.
The statement to assign Hello World to the variable message will be executed and the value
in variable message should be Hello World now. Refer the below picture for more clarity:
http://www.learnexcelmacro.com Page 14
10. Similarly keep pressing F8 and each statement will keep executing and you can easily monitor
each of the flow of the program.
Note: When your debugging is over and you want program to run in one go then press F5. It will
run the remaining statements in one go.
Now you know how and where to write Excel Macro in Excel. Also you learnt how to run a program in
debug mode. While running the program in debug mode, you got to know how to add Watch in Excel
VBA.
You might have seen in excel, some command buttons, text boxes, combo boxes etc. which looks very
interesting. It gives a better User friendly interface to the user and also it gives you a nice feeling. So
are you ready to know how to user command button in Excel.
How to use Activex Controls like Button, Text Box etc. in Excel:
First we will see how to add a command button in your excel worksheet. Follow the below steps to
add command button in excel sheet.
http://www.learnexcelmacro.com Page 15
1. Select the Developer Tab in your Excel sheet
2. Go to Insert Activex Controls Command button
3. Click on the Command Button and Click on the sheet where you want to place your Command
Button.
4. Nice your command button is here. But now you are thinking the Caption of this button is
not helpful. User will not understand what this buttons will after clicking on it. So dont worry
you can give your own name and caption to this button. As we have already discussed about
the Properties window of the VBA Code window. Now since you are on your excel sheet not
on the VBA code window, so you need to click on Properties button available in the Ribbon
or you can right click on the Command Button object and Choose the option Properties as
shown below:
http://www.learnexcelmacro.com Page 16
Note: From this Properties window you can change the Font Colour, Back Colour etc of your any
Object. Like in the above case, it is Command Buttons.
You have placed your Command Button in your sheet. Now lets focus on how to write a code for this
button. It means once you click on this button, some operations must be performed.
Once you double click on the command button, you will be automatically to a Click Event of the
button. Here you between Sub and End Sub you can write your code same as you have seen in the
previous example of SelectionChange event of the worksheet. Type this statement MsgBox "Hello
World" between Sub and End Sub.
End Sub
http://www.learnexcelmacro.com Page 17
Now we need to run the program and see if it is functioning properly.
To run this command button, we need to come out of the Design Mode. You would have noticed that
as soon as you added the Command Button, Excel macro will go in Design Mode.
Once you come out of the design mode, Click on the Command button. As soon as you click on that,
once message box will display with a message Hello World as shown in the below image.
Similarly you can add Text Boxes, List Boxes, Combo Boxes etc. Also you can write the code for them.
To know more about the Commandbutton and other controls read the Articles which is available on
clicking on the below link:
Combobox: http://www.learnexcelmacro.com/2012/03/excel-macro-tutorial-combobox-in-excel-
macro/
http://www.learnexcelmacro.com Page 18
How to Record Macro in excel
In this section you will learn, how to Record Macro in Excel. Follow the below Steps one by
one to record macros in Excel:
4. In Excel 2007 and 2010, Macro recording can be started by clicking on the Red Shape
button marked in the bottom bar of the Excel in left hand side.
http://www.learnexcelmacro.com Page 19
Note:
6. Now click OK and do some operation on the excel sheet. Like formatting of the cell or
sorting some of the values etc.
7. Once the macro starts recording then the sign of the recording button gets changed as
shown in the image ahead.
8. Now whatever operation you do on Excel, it will get recorded.
For example: Do some formatting on the Sheet1. Once the formatting done then again
Click on the same button as highlighted to stop the recording. Following is the
formatting which I have done on the sheet 1.
9. I have done the above formatting and wrote few column names while recording.
10. To view this particular recorded macro, right click on any of the Sheet Name -> View
Code
11. In left hand side pane, under VBA Project of that workbook, Expand the Module
12. One Module would have been created there Module 1. Click on this.
13. You can see the recorded code with the given Macro Name.
http://www.learnexcelmacro.com Page 20
Now your macro is recorded. Now let us see how to run the Recorded Excel Macro.
1. To run this Macro, I have deleted the formatting done during the recording of this macro.
Now once I will run this Macro, then automatically that formatting will be done in that Sheet.
http://www.learnexcelmacro.com Page 21
2. Now Click on the Run Button to Run the Written Macro.
http://www.learnexcelmacro.com Page 22
4. After running the Macro, See the Result.
If you do not want to run the Macro, by Pressing the Run Button in VBE, then follow the
below steps:
1. Press Alt + F8
2. Select Your Macro from the Drop Down
3. Click on Run
http://www.learnexcelmacro.com Page 23
Thank you
Click to Send a Feedback to Me
Thank you so much for reading this e-Book. Kindly spare few minutes to write
a feedback.
Do give me feedback on this. Based on your feedback soon the next version of
the free e-book will be released.
http://www.learnexcelmacro.com Page 24