VBAStudent2010
New Member
- Joined
- Dec 16, 2012
- Messages
- 2
Help. I am trying to value a treasury bond with
Face Value =1000
Market Price = 960
4 Years until Maturity
Coupon Rate = 7%
When I enter the below code and try to solve for YTM. I get an error saying "runtime error 6 - overflow"
it says it about half way down where "Dif = P - (Cv * SumIt(N, x) + F / x ^ N)" appears. Could anyone help me get past this?
Do you notice any other issues with the code?
Thanks
Face Value =1000
Market Price = 960
4 Years until Maturity
Coupon Rate = 7%
When I enter the below code and try to solve for YTM. I get an error saying "runtime error 6 - overflow"
it says it about half way down where "Dif = P - (Cv * SumIt(N, x) + F / x ^ N)" appears. Could anyone help me get past this?
Do you notice any other issues with the code?
Thanks
Code:
Sub YieldToMaturity()
Dim F As Double, Cr As Double, Cv As Double, N As Integer, P As Double, Dif As Double
Dim Inc As Double, Ct As Long, YTM As Double, x As Double, curYld As Double
With ActiveSheet
F = Cells(1,2).Value
Cr = Cells(2,2).Value
Cv = F * Cr
P = Cell(3,2).Value
N = Cells(4,2).Value
curYld = Cv / P
Inc = 0.00001
'First pass trial value for YTM
Select Case curYld
Case Is > Cr
YTM = curYld + Inc
Do
Ct = Ct + 1
x = 1 + YTM
Dif = P - (Cv * SumIt(N, x) + F / x ^ N)
YTM = YTM + Inc
Loop While Ct <= 100000 And Dif < 0
Case Is < Cr
YTM = curYld - Inc
Do
Ct = Ct + 1
x = 1 + YTM
Dif = P - (Cv * SumIt(N, x) + F / x ^ N)
YTM = YTM - Inc
Loop While Ct <= 100000 And Dif > 0
Case Else
YTM = curYld
End Select
End With
MsgBox "YTM is: " & Round(YTM * 100, 2) & "%" & vbNewLine & "After " & Ct & " iterations"
End Sub
Function SumIt(N As Integer, x As Double) As Double
For k = 1 To N
SumIt = SumIt + 1 / (x) ^ k
Next k
Last edited: