我对VBA比较陌生。
运行以下代码时,我收到“下标超出范围”错误。尝试从创建的函数调用值时发生错误,请参阅第21至24行
任何帮助将不胜感激!
由于
Public Sub MtgLnYTM()
Dim cpn As Single, maturity As Integer, price As Integer, mtgprice As Integer, mort_price() As Double, yield_calc() As Double, calc_price As Integer
Dim row As Integer
'asks user for Coupon rate
cpn = InputBox("Please enter the coupon rate of the security(in X.XX form)")
If -0.0001 < coupon < 25.0001 Then
Cells(1, 2).Value = cpn
End If
'asks user for number of years until maturity
maturity = InputBox("Please enter the periods until maturity")
If 179 < maturity < 361 Then
Cells(2, 2).Value = maturity
End If
'asks user for yield rate
mtgprice = InputBox("Please enter the Mortgage price per $100 principal value, in percent")
If 49.9999 < Yield < 200.0001 Then
Cells(3, 2).Value = mtgprice
End If
For row = 6 To 16
Cells(row, 2).Value = mort_price(mtgprice, cpn, maturity)
Cells(row, 1).Value = yield_calc(mtgprice, fderiv(mtgprice, cpn, maturity), mort_price(mtgprice, cpn, maturity))
Next row
Dim mychart As ChartObject
Dim mylo As Integer, myhi As Integer
mylo = -1 + WorksheetFunction.Min(Range("B6:B16"))
myhi = 1 + WorksheetFunction.Max(Range("B6:B16"))
Set mychart = Sheets("Sheet1").ChartObjects.Add _
(Left:=300, Top:=25, Width:=400, Height:=300)
With mychart
.Chart.ChartType = xlXYScatterLines
.Chart.HasTitle = True
.Chart.ChartTitle.Text = "Price of a " & maturity & " month mortgage loan with an annual coupon rate of " & cpn & "%"
.Chart.SetSourceData Source:=Range("A6:B16")
.Chart.Axes(xlValue).MinimumScale = mylo
.Chart.Axes(xlValue).MaximumScale = myhi
.Chart.Legend.Clear
End With
End Sub
答案 0 :(得分:-1)
根据我的统计数字&#33; 21到24&#39;如下:
如果49.9999&lt;产率&lt; 200.0001然后
单元格(3,2).Value = mtgprice
结束如果
看起来很好。
如果您的函数mort_price,yield_calc或fderiv未正确声明,您将获得&#34;下标超出范围&#34;
你也会得到&#34;下标超出范围&#34;如果一个名为&#39; Sheet1&#39;的工作表已存在于您的工作簿中。