You are on page 1of 20

Creating Custom

Worksheet Functions
Prof. Anurag Saxena
What is a function?

• Function is combination of lines of code that accomplish a


specific task

• Functions usually takes data, process the data, and return the
result

• From user’s perspective, there are two types of functions

• Built-in functions: These functions are already specified in an application


and can be used by a programmer. For example, Sum function in Excel
• User defined function: These are the functions that are defined by the
users. User-defined-functions are used to specify a customized task that
the programmer wish to accomplish.
How to define a function?

• Function is specified as

Function Function_Name (argument1 as type…..,argument n as type) as type


How to define a function?

• Function is specified as

Function Function_Name (argument1 as type…..,argument n as type) as type

Signifies to VB
beginning of a
user defined function
How to define a function?

• Function is specified as

Function Function_Name (argument1 as type…..,argument n as type) as type

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 Function_Name (argument1 as type…..,argument n as type) as type

Signifies to VB The data provided


beginning of a as input
user defined function
Any number of
arguments
Every function can be specified
should be given
a name
How to define a function?

• Function is specified as

Function Function_Name (argument1 as type…..,argument n as type) as type

Signifies to VB The data provided


beginning of a as input The type of data
user defined function inputted. For eg,
Any number of
arguments Integer, String
Every function can be specified
should be given
a name
How to define a function?

• Function is specified as

Function Function_Name (argument1 as type…..,argument n as type) as type

Signifies to VB The data provided


beginning of a as input The type of data The type of value,
user defined function inputted. For eg, the function will
Any number of
Integer, String Return.
arguments
Every function For eg. Integer,
can be specified
should be given String
a name
How to define a function?

Function Function_Name (argument1 as type…..,argument n as type) as type

Statement1
Statement 2
.
.
.
Statement n

End Function

Please note: A function can have 0 to any number of arguments


How to use a function

Step 1: Open VBA editor (Alt, F11 Key)


Step 2: Create module
Step 3: Write function (Enter the keyword ‘Function’ and insert the
VBA code, then End the function with an End Function statement
Step 4: Go to the Excel sheet
Step 5: From any cell call the function: =Function_Name(arg1,…)

A good practice is to specify arguments in excel cells, then provide


the cell address as input in Function.
VBA language elements

• 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

• Control structures are used to control the flow of VBA program


• Commonly used control structures are

• 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

• Select Case: Depending upon the value of an expression, it executes


one from a group of statements

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 ……Next : To repeat a set of • Initially the code, Set i =1 and


statement execute the instruction,

Eg: • At the end of for instruction Next i


statement increases the value of I
by 1 and then pass on the control
For i = 1 to 5 Step 1 to For statement.
Sum = Sum + i
Next i • For statement checks to see if the
value of i is less than equal to 5,
• If it is then sum instruction is
executed,
• Otherwise for loop is ended.
Control Structure

• Do……Until: Repeats the statements until a condition is true

For Eg:

Do Until S > 0 ‘beginning of Do Until


K=K+1
S = S -1
Loop ‘End of Do Until
Demo Problems

• 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

How to Call this function in Excel:


= leapyear(B1)
Where Cell B1 has the input year
Demo Problems

• Compute number of days in a month How to Call this function in


Excel:
Function Monthday(m As Integer, yr As
Integer) As Integer = Monthday(A1,B1)
Select Case m Where Cell A1 has month
number (1, 2, 3 etc)
Case 1, 3, 5, 7, 8, 10, 12
Monthday = 31 B1 has the input year (2010,
Case 4, 6, 9, 11 2012 etc)
Monthday = 30
Case 2
Note: This function is calling
leapyear function.
Monthday = 28
If LeapYear(yr) Then Monthday = 29 leapyear function should be
End Select copy pasted in the same module
End Function below Monthday function
Demo Problems

• Reverse characters in a string • How to Call this function in


Excel:
Function reverse (T as string) as string
Dim i as Integer = Reverse(B1)
Reverse =“”
For i = Len(T) to 1 Step -1 Where in Cell B1 is the string
whom you wish to reverse.
Reverse = Reverse & Mid (T, i, 1)
Next i
End Function
Demo Problems

• Computing factorial of a • How to Call this function in


number Excel:

Function Fact( N as Integer) =Fact(B1)


If N =1 Then
Fact=1 Where in cell B1 is the number
Else whose factorial you wish to find
Fact = N * Fact(N-1) out.
End IF
End Function Here Note the implementation of
recursive function (recursion)
THANK YOU

You might also like