Académique Documents
Professionnel Documents
Culture Documents
By
Vishnupriyan Anantharaman
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.
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.
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
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
Dim variable
Dim variable As type
If a variable is not declared it will be created when used, the type will be Variant
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
Else
End If
Case expr1
Case expr2
Case Else
End Select
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