Sunday, 15 June 2014

My First VBA Code (Macro)

My First VBA Code (Macro)

For Beginners

This Article helps beginners to write their first Macro in VBA.

Watch Video

People thinks writing a Macro/VBA codes is very difficult however this is not true and anybody can learn VBA by follow some Basic instructions.

VBA (Visual Basic for Applications): As it's name suggest it is extension of VB which is designed by Microsoft for Microsoft applications, such as Excel, Access, Word etc. It's a macro programming language, which used to automate excel and other Microsoft application. To Automate Excel you need to write Code (VBA script) in VBA Editor.
 
You can Access VBA Editor by just pressing Alt+F11 (Shortcut Key) or clicking on Visual Basic under Developer Menu Bar.
  

 


Now you will be able to see VBA Editor, where you can write your first VBA script for your Macro.




To write a code you need to insert a Module. To open the Module you need to go to the Insert Menu Bar and then select Module.



Here is your first Module.



Now you can write your codes. However before moving ahead we need to understand composition of VBA procedure.

As VBA is a structured Language hence, we need to follow its basic composition.

Composition of VBA Script:
A VBA module consists of Sub procedures. A VBA procedure is constructed from three types of statements which needs to be write between Sub and End Sub Statement
Executable: These Statement use to perform an Action, the actual task which you want to perform.
Declaration: This statement is used to define procedure, variables, properties, arrays and constant.  
Assignment: Assignment statements carry out assignment operations, which consist of taking the value on the right side of the assignment operator (=) and storing it in the element on the left.
Now we learnt about basic structure of VBA script, Let’s move ahead and write a program.
To begin with, we will create a Message Box Program, following are the Steps.
Steps:
1.    In Module, Type “Sub <Name of your procedure> ()”, Press Enter.
2.    Now Type Msgbox “Hello”
3.    Your module will look like as given below


 
4.    To Execute your program, Press F5




 
         5.    Now you have created your first Macro and it has been executed successfully.

Click Here to download the excel file along with the codes.


You can share your feedback and post your excel queries in this blog.

No comments:

Post a Comment