Basics of Macro and VBA
-
Upload
vishnuanath -
Category
Documents
-
view
229 -
download
0
Transcript of Basics of Macro and VBA
-
8/10/2019 Basics of Macro and VBA
1/16
-
8/10/2019 Basics of Macro and VBA
2/16
Macro! What is it?
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 youdo 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 t
great extent, you can do anything here, get inputs from user, give output etc.,
VBE uses the coding language VBA.
Macro
-
8/10/2019 Basics of Macro and VBA
3/16
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 Exc
In Excel 2007, you need to choose the option Developer->Macro Security->Ma
Settings->Enable all macros
To Create a Macro the short cut key is Alt + F8 and to open the VBA the short cut kF11, and they both are available under the Developer menu in Excel 2007.
-
8/10/2019 Basics of Macro and VBA
4/16
Visual Basic Editor (VBE)
VBE can be sub dividethree windows, they ar
1. Project Window
2. Properties Window
3. Code Window
Lets see each window i
-
8/10/2019 Basics of Macro and VBA
5/16
VBE-Project Window In Project window you can
various workbooks that are If we expand, we can see t
sheets in each workbook.
We can also see the modul
forms that we create for a w
If you click on the sheet, t
properties window will shoproperties of that sheet.
And Code window will sho
written for that sheet. If n
written, that it will be blank
-
8/10/2019 Basics of Macro and VBA
6/16
VBEPROPERTIES WINDOW
Properties Window will show the pro
of the object that we have selected in
Project Window by clicking on it.
Here you can see that there are totall
properties. You can modify the prop
according to your needs.
Now as highlighted in Left hand side
you change the value of the property
Name as Introduction instead of
Sheet1 you can see that in Excel thcorresponding sheet name would hav
changed
The code in VBA that is used to cha
the sheet name isActivesheet.Nam
"Introduction"
-
8/10/2019 Basics of Macro and VBA
7/16
VBECODE WINDOW
Double click on workbook in the
project window, the code window open as given in left hand side.
You can straight away start typing
VBA script in the blank space.
You can see two drop down box i
top, the one at left is showing Gen
now. If you try to expand, it will s
Workbook as one option, which ycan choose. As you have clicked o
Workbook in Project window.
After choosing workbook from th
dropdown box, the first and last li
macro will come
-
8/10/2019 Basics of Macro and VBA
8/16
Code Window continue.
-
8/10/2019 Basics of Macro and VBA
9/16
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 wo
in opened.
Now as seen in the previous slide there is one more drop down box in the top right, if you
that, you will find some 28 events. You can choose any of the event, the corresponding fir
line of the macro will appear, and any code you place between that line, will run when the e
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 acti
-
8/10/2019 Basics of Macro and VBA
10/16
VBA GrammarVisual Basic for Application is an Object Oriented Language. This means that programmi
mean that dealing with objects, methods and there properties.
ObjectsEvery item present in excel are objects. An object can contain other objects. Application is
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)
MethodsMethods can be defined as the actions, which can be performed on the objects. Some
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 consider
would be Value, Font, Column & row width, Formula etc.
-
8/10/2019 Basics of Macro and VBA
11/16
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 variableAs 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 bepublicorprivate
Variables
-
8/10/2019 Basics of Macro and VBA
12/16
Basic Syntax for VBAThis 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 she
cell A1 has the value Infy and the pointer points to A1 then Activecell.value would return In
Activesheet.cells(x,y) :- this one gets the value of the x,y cell where x & y point to row and colu
sheet1.cells(x,y) :- this one points to the x,y cell in sheet1 where sheet1 might be defined as said
part.
sheet1.Range(A1:C3).select :- this syntax would select the cells from A1 to C3 is similar to clic
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 rathis case A1 to C3
sheet1.cells(1,1) .Paste Special Paste:=xlPasteValues :- this line is the simulation of the right clic
functionality in excel.
Activesheet.Paste :- this one acts similar to the paste option in excel. Before using this code Act
has to be used without using it would cause an error.
-
8/10/2019 Basics of Macro and VBA
13/16
Programming Statements Logical statements
The If Then Else statement is the basic logic test
If a>10 Then
End If
If a>10 Then
ElseIf a10 Then
Else
End If
-
8/10/2019 Basics of Macro and VBA
14/16
The Select statement can be used to replace a multi-way if statement
Select Case expression
Case expr1
Case expr2
Case Else
End Select
-
8/10/2019 Basics of Macro and VBA
15/16
Dealing with runtime errors The On Error statement will trap errors
The errorname is a label in the code
In the error code a Resumestatement will cause the statement that caused th
error to be executed again
In the error code a Resume Nextstatement will restart execution on the
statement after the one that caused the error
On Error GoTo label
On Error GoTo check
check:
-
8/10/2019 Basics of Macro and VBA
16/16
Thankyou