首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下标超出了函数的范围?

下标超出了函数的范围?
EN

Stack Overflow用户
提问于 2016-10-27 06:28:00
回答 1查看 177关注 0票数 0

我是VBA的新手。

运行以下代码时,我收到"Subscript out of range“错误。尝试从创建的函数调用值时出现错误,请参阅第21行到第24行

任何帮助都将不胜感激!

谢谢

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-10-27 13:34:44

根据我的计数,'21到24‘行如下所示:

如果49.9999 <收益率< 200.0001,则

单元格(3,2).Value = mtgprice

结束If

这看起来很好。

  1. 如果您的函数mort_price、yield_calc或fderiv未正确声明,则在工作簿中不存在名为'Sheet1‘的工作表时,您将获得"Subscript out of range“
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40273127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档