Vous êtes sur la page 1sur 16

Basics Of Macro and VBA

By
Vishnupriyan Anantharaman

Macro! What is it?

Macro

Macro is a combination of lot of task that you can record once and then replay
that as many times as possible just by a single click. Almost everything that you
do in Excel can be done using Macro. It is very simple that everyone can do this,
even without scripting knowledge

If you simply record some thing and replay that, then it is not of much use.
Excel Macro, uses VBE to make the macro more dynamic and useful.

Now what is VBE?


Visual Basic Editor is a part of Excel, where you code and create tools, which
will make your work ease, and will do wonders by reducing the effort and time to
great extent, you can do anything here, get inputs from user, give output etc.,
VBE uses the coding language VBA.

Macro settings to be followed


Before starting with Macro or coding in excel, the first thing that you should do is to:

set the security level as Low or Medium so that the Macro will work in Excel 97-2003,
In Excel 2007, you need to choose the option Developer->Macro Security->Macro
Settings->Enable all macros

To Create a Macro the short cut key is Alt + F8 and to open the VBA the short cut key is Alt +
F11, and they both are available under the Developer menu in Excel 2007.

Visual Basic Editor (VBE)


VBE can be sub divided into
three windows, they are:

1. Project Window

2. Properties Window
3. Code Window
Lets see each window in detail

VBE-Project Window
In Project window you can see the
various workbooks that are open

If we expand, we can see the various


sheets in each workbook.

We can also see the modules and


forms that we create for a workbook.

If you click on the sheet, the


properties window will show the
properties of that sheet.

And Code window will show the code


written for that sheet. If no code is
written, that it will be blank.

VBE PROPERTIES WINDOW


Properties Window will show the property
of the object that we have selected in the
Project Window by clicking on it.

Here you can see that there are totally 12


properties. You can modify the properties
according to your needs.

Now as highlighted in Left hand side, If


you change the value of the property
Name as Introduction instead of
Sheet1 you can see that in Excel the
corresponding sheet name would have
changed

The code in VBA that is used to change


the sheet name is Activesheet.Name =
"Introduction"

VBE CODE WINDOW


Double click on workbook in the
project window, the code window will
open as given in left hand side.

You can straight away start typing the


VBA script in the blank space.

You can see two drop down box in the


top, the one at left is showing General
now. If you try to expand, it will show
Workbook as one option, which you
can choose. As you have clicked on
Workbook in Project window.

After choosing workbook from the left


dropdown box, the first and last line of
macro will come

Code Window continue.

Code Window continue.


As you can see in previous slide you would be able to see the code:
Private Sub Workbook_Open()
End Sub

What ever code you place between these two statement, will start running as soon as the workbook
in opened.

Now as seen in the previous slide there is one more drop down box in the top right, if you expand
that, you will find some 28 events. You can choose any of the event, the corresponding first and last
line of the macro will appear, and any code you place between that line, will run when the event will
be triggered.

For eg., if you choose Activate event, the following code will appear
Private Sub Workbook_Activate()
End Sub

So any VBA code between the above two statement will run whenever the workbook is activated.

VBA Grammar
Visual Basic for Application is an Object Oriented Language. This means that programming in VBA would
mean that dealing with objects, methods and there properties.
Objects
Every item present in excel are objects. An object can contain other objects. Application is the largest object
containing workbooks, the hierarchy follows.
For ex: - Application (Includes all open workbooks)
Workbook (Includes all worksheets in the particular workbook)
Worksheets (Includes all the cells in the worksheet)
Range (Includes the selected cells)
Methods
Methods can be defined as the actions, which can be performed on the objects. Some examples are like
Activate, Copy, Clear, Paste, etc. can be performed on a worksheet, cell, range etc.
Properties
Properties are the characteristics of an object. For example if a particular cell is considered the properties
would be Value, Font, Column & row width, Formula etc.

Variables

Variables
must start with a letter
can contain _ and numbers
cannot exceed 255 characters in length

Within a procedure declare a variable using

Dim variable
Dim variable As type

If a variable is not declared it will be created when used, the type will be Variant

Use Option Explicit in the declaration section to require declaration of variables.

VBA variables have scope restrictions


variables declared in a procedure are local to that procedure
variables declared in a module can be public or private

Basic Syntax for VBA


This part has some syntax which a starter can use.

Activecell.value:- this syntax gets the value of the particular cell which is active in the active sheet. For example if
cell A1 has the value Infy and the pointer points to A1 then Activecell.value would return Infy.

Activesheet.cells(x,y) :- this one gets the value of the x,y cell where x & y point to row and column.

sheet1.cells(x,y) :- this one points to the x,y cell in sheet1 where sheet1 might be defined as said in Assign Syntax
part.

sheet1.Range(A1:C3).select :- this syntax would select the cells from A1 to C3 is similar to click on A1 and drag
till C3. this same select statement can be used to select a cell too.

sheet1.Range(A1:C3).copy :- this line would copy the values and other format in the selected range of cells i.e. in
this case A1 to C3

sheet1.cells(1,1) .Paste Special Paste:=xlPasteValues :- this line is the simulation of the right click and Paste Special
functionality in excel.

Activesheet.Paste :- this one acts similar to the paste option in excel. Before using this code ActiveSheet.select code
has to be used without using it would cause an error.

Programming Statements

Logical statements
The If Then Else statement is the basic logic test
If a>10 Then

End If
If a>10 Then

Else

End If
If a>10 Then

ElseIf a<0 Then

Else

End If

The Select statement can be used to replace a multi-way if statement


Select Case expression

Case expr1

Case expr2

Case Else

End Select

Dealing with runtime errors


The On Error statement will trap errors

On Error GoTo label

The error name is a label in the code

On Error GoTo check

check:

In the error code a Resume statement will cause the statement that caused the
error to be executed again

In the error code a Resume Next statement will restart execution on the
statement after the one that caused the error

Thank you

Vous aimerez peut-être aussi