Bond Valuation using VBA

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

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:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
After changing the line P = Cell(3,2).Value to P = Cells(3,2).Value it worked fine for me.
with:
B1=1000
B2=0.07
B3=960
B4=4

it gave me YTM=8.21%
 
Upvote 0
After changing the line P = Cell(3,2).Value to P = Cells(3,2).Value it worked fine for me.
with:
B1=1000
B2=0.07
B3=960
B4=4

it gave me YTM=8.21%

Even after this correction, the same error appears. It still points me to the first time it says "Dif = P - (Cv * SumIt(N, x) + F / x ^ N)".
Any advice?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top