Hi guys,
I'm new to vba and recently I met a challenge in my work and I really appreciate if someone can give me some suggestions.
So I'm trying to write a function to calculate a Bond's Price.
Given values: coupon, maturity, yield.
It will be something like this BondPrice=(coupon,maturity,yield)
And this is the mathematics formula :
Price (yield)= ∑_(i=1)^(maturity)▒〖CF〗_i/〖( 1+yield)〗^i
note: 〖CF〗_i= (coupon)*100
The price is a sum from period i = 1 to maturity, given coupon, maturity and yield.
Specifically,
BondPrice=P.1+P.2+P.3+...+P.maturity
That sigma in the formula get me busted so hard because of the 'rule' we've given.
Rule:
Required to write the function without calling vba functions such as P=Sum(Range()). Yeah pure vba language!!!
-------------------------------------------------------------------------------------
Below is my 3-hours attempt and it wouldn't work as well as violated the rule.
Public Function BondPrice(alpha, beta, gama)
'alpha=coupon
'beta=maturity
'gama=yield
Dim matrix(10 To 10, 1 To 100) As Double
alpha = Cells(3, 3).Value
beta = Cells(4, 3).Value
gama = Cells(5, 3).Value
'their values are gathered from a subroutine that I wrote before, I put them here for testing purpose.
For i = 1 To beta
matrix(10, i) = ((alpha ) * 100) / (1 + (gama ) ^ i)
Next i
BondPrice = Application.Sum(Range(matrix(10, 1), matrix(10, beta )))
End Function
---------------------------------
The work is due next Thursday. That could mean I have a lot of time to enjoy my frustration here.
But I trust the community to be my salvation. Please help! And please don't forget the rule!
Bookmarks