Bond Valuation using VBA

VBAStudent2010

New Member
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

iyyi

Active Member
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%

VBAStudent2010

New Member
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)".

Last edited:

Replies
1
Views
65
Replies
1
Views
690
Replies
1
Views
91
Replies
4
Views
225
Replies
0
Views
171

1,186,556
Messages
5,958,475
Members
438,361
Latest member
mihi78

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.

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

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