0% found this document useful (0 votes)
30 views

UserDefined Functions

This document discusses how to create user-defined functions in VBA. It provides two examples: 1) a public function that calculates future value based on present value, interest rate, and number of years using compound interest, and 2) a public function that takes an exam mark as input and returns the corresponding letter grade as output using a select case statement. The functions are called from private subs to demonstrate their usage.

Uploaded by

aseret423
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

UserDefined Functions

This document discusses how to create user-defined functions in VBA. It provides two examples: 1) a public function that calculates future value based on present value, interest rate, and number of years using compound interest, and 2) a public function that takes an exam mark as input and returns the corresponding letter grade as output using a select case statement. The functions are called from private subs to demonstrate their usage.

Uploaded by

aseret423
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating User-Defined Functions

14.1 Creating Your Own Function


The general format of a function is as follows:
Public Function functionName (Arg As dataType,..........) As dataType
or
Private Function functionName (Arg As dataType,..........) As dataType
* Public indicates that the function is applicable to the whole project and
Private indicates that the function is only applicable to a certain module or procedure.


Example 14.1
In this example, the user can calculate the future value of a certain amount of money he
or she has today based on the projected interest rate and the number of years from
now, supposing he or she will invest this amount of money somewhere .The calculation is
based on compound interest rate.




The code
Public Function FV(PV As Variant, i As Variant, n As Variant) As Variant
'Formula to calculate Future Value(FV)
'PV denotes Present Value

FV = PV * (1 + i / 100) ^ n
End Function
Private Sub compute_Click()
'This procedure will calculate Future Value
Dim FutureVal As Variant
Dim PresentVal As Variant
Dim interest As Variant
Dim period As Variant
PresentVal = PV.Text
interest = rate.Text
period = years.Text
'calling the funciton
FutureVal = FV(PresentVal, interest, period)
MsgBox ("The Future Value is " & FutureVal)
End Sub



Example 14.2
The following program will automatically compute examination
grades based on the marks that a student obtained. The code is
shown on the right.

The Code

Public Function
grade(mark As
Variant) As String
Select Case mark
Case Is >= 80
grade = "A"
Case Is >= 70
grade = "B"
Case Is >= 60
grade = "C"
Case Is >= 50
grade = "D"
Case Is >= 40
grade = "E"
Case Else
grade = "F"
End Select
End Function

Private Sub
compute_Click()
grading.Caption =
grade(mark)

End Sub

You might also like