Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Macro command
Record a Macro for typing the following and then applying the
formulas written below
A
S.No
Name
Marks
Amit
56
Kamal
67
Namrata
68
Deepti
59
Yogesh
78
Macro command
Macros/Alt+F8
Run Button
Programming
Nowadays computer programming has become
much easier, and even soso-called normal people
now engage in this activity. Programming
simply means developing instructions that the
computer automatically carries out.
Excel programming means that you can instruct
Excel to automatically do things that you
normally do manually saving you lots of time
resources.
About VBA
VBA is the best thought of as Microsofts
common application.
It is included with most of the Office 2003
applications.
You will be able to write macros for other
Microsoft products.
Basics of VBA
Code: You can perform actions in VBA by
executing VBA code. You write ( or
record) VBA Code which is stored in a VBA
Module.
Module: VBA Modules are stored in a
Excel workbook, but you can view and edit
a module by using Visual basic Editor
(VBE).
VBE
The Visual Basic Editor is a separate
application where you write and edit your
VBA macros. It works seamlessly with
Excel. You can activate the VBE
by pressing Alt+F11 when Excel is active
or by using the Tool
Tool--Macro
Macro--Visual Basic
Editor command.
command.
Project
Explorer
Properties
Window
Code
Window
Project Explorer
The Project Explorer window displays a tree
diagram that shows every workbook currently
open in Excel (including addadd-ins and hidden
workbooks). When youre working in the VBE,
each Excel workbook and addadd-in thats open is a
project. You can think of a project as a collection
of objects arranged as an outline. Click on the
plus sign (+) at the left of the projects name in
the Project Explorer window to expand a project.
Code Window
The VBA code is contains in the Code
window. Every object in a project has an
associated Code window. DoubleDouble-click the
object in the Project Explorer window to
bring up the Code Window. For example,
to view the Code window for the Sheet1
object, double
double--click Sheet1 in the Project
Explorer window.
window. Unless youve added
some VBA code, the Code window will be
empty.
Object Browser
The VBE includes another tool, known as the Object Browser. This
tool allows you browse through the objects available to you. To
access the Object Browser, press F2 when the VBE is active (or
choose View
Object Browser).
Object
Browser
Properties Window
This window is
used to show/Edit
properties of the
selected object in
the current project.
eg: Name, Height,
Font, Enable,
Visible etc.
Immediate Window
The Immediate window is the most useful for executing
VBA statements directly testing statements, and
debugging your code. This window might or might not
be visible. If the Immediate window is not visible then
press CTRL+G. To close click the Close button in its title
bar or right click anywhere in Immediate Window and
select Hide from the shortcut menu.
Immediate
Window
Basics of VBA
Module
Procedures
Sub-Procedure
Function-Procedure
Sub--Procedure
Sub
A Sub procedure consists of a series of
statements and can be executed in
number of ways.
Eg: Sub Test ()
Sum=1+1
Msgbox the answer is & Sum
End Sub
Function--Procedure
Function
Function: A VBA Module can also have
Function procedures. A Function
procedure returns a single value. A
Function can be called from another VBA
procedure or used in a worksheet formula.
Eg: Function AddTwo ( arg1, arg2)
AddTwo = arg1 + arg2
End Function
WorkBooks
WorkBook
Sheets
Worksheet
Range
What is Variable
A Variable is simply a named storage
location in your computers memory.
Variables can accommodate a wide variety
of data types from simple Boolean to
large, precision values. You can assign a
value to a variable by using an equal sign
operator.
Eg: name=Ajay , amt=4500
Data Types
When a data type is
declared it means
that you have define
the kind of value that
may be stored within
the memory allocated
for a variable. There
are many data types
BytesUsed
Range of Values
Boolean
2 Bytes
True or False
Integer
2 Bytes
-32,768 to 32,767
Long
4 Bytes
-2,147,483,648 to 2,147,483,647
Date
8 Bytes
String
Lengthof
String
1 to 65,400 characters
Variant
16 Bytes
+
*
/
^
Eg:
Dim myName As String
myName = Peter Anderson
Relational operators
=
<>
<
>
<=
>=
Truth table
AND
Condition1
True
True
False
False
Condition2
True
False
True
False
True
False
False
False
Condition2
True
False
True
False
Condition1 OR Condition2
True
True
True
False
NOT Condition1
False
True
Eg:
If (userGuess <> answer) Then
MsgBox (Wrong! The answer is & answer)
Else
MsgBox (You got it!)
End If
ElseIf clause
Another option regarding If/Then/Else structures is the ElseIf clause. The ElseIf
clause is used like the Else clause with a conditional expression. You can use the
if...then...elseif statement if you want to select one of many blocks of code to
execute: Look at the example below,
MsgBox ("Unknown")
Lets create a procedure that reads the value in the cell A4, and puts
the same value in the cell A5
Lets create a procedure that changes the value in the cell B9 to the
current time every time you click on the button.
Create a workbook specific code so that every time you open the
excel file you are getting a welcome message and every time you
are closing this file you are getting a bye message
Lets create a procedure which change the color of the cell based on
the value selected in it. Just like conditional formatting
First Name
LastName
Full
Gupta
?
?
?
?
?
Sharma
Mehta
Lets create a form like this, which has a button once you click on
the button a message is displayed on the screen
Create a form like this which appears every time you open the file
Name
:
Manish Beniwal
Mobile no.
:
9891242247
Email
:
manishsinghbeniwal@yahoo.com