Professional Documents
Culture Documents
Worksheet Functions
Prof. Anurag Saxena
What is a function?
• Functions usually takes data, process the data, and return the
result
• Function is specified as
• Function is specified as
Signifies to VB
beginning of a
user defined function
How to define a function?
• Function is specified as
Signifies to VB
beginning of a
user defined function
Every function
should be given
a name
How to define a function?
• Function is specified as
• Function is specified as
• Function is specified as
Statement1
Statement 2
.
.
.
Statement n
End Function
• Data Types: VBA support several data types -> Boolean (0,1), Integer,
Long, Double, String etc
• Variable declaration: Variables are declared using “Dim” statement by
assigning name and data types
• For eg: Dim Tax as integer, Dim I as const
• Constants are declared using “Const” statement
• For eg: Const Tax = 10
• Arithmetic operators: “+” , “-” , “*” , “/” , “^” (exponential), Mod
(remainder of a division)
• Logical Operators: And, Or, Not
• Comments: You can insert comments by using single quote (‘). It is used
for providing narrative information in the code.
Control Structure
• If Then
If <Condition> Then If <Condition> Then
<Statements> <Statement>
End If
• If Then Else
If <Condition> Then
<Statements>
Else
<Statements>
End If
Control Structure
Eg: Depending on the value of m, the below case structure finds the
number of days in a month
Select case m
Case 1, 3, 5 ,7, 8 ,10, 12
Monthdays = 31
Case 4, 6, 9, 11
Monthdays = 30
Control Structure
For Eg:
• Leap year is any year that can • The below Function checks if a
given year is a leap year or not
be exactly divided by 400 (such Function leapyear(yr as integer) As
as 2000, 2400) Boolean
leapyear = False
If (yr Mod 400) = 0 Then
leapyear = True
• Leap Years are any year that can Else
If (yr Mod 4) = 0 Then
be divided by 4 (such as 2016, If (yr Mod 100) <>0 Then
2020, 2024) but not by 100 leapyear = True
End If
(such as 2100, 2200) End If
End If
End Function