简体   繁体   English

VBA运行时错误9(Excel 2007)

[英]VBA Runtime Error 9 (Excel 2007)

i have got the following problem. 我有以下问题。 I am creating an excel worksheet with active x elements to calculate several values (for a class in university). 我正在创建一个具有活动x元素的excel工作表,以计算多个值(针对大学中的一门课)。 And in the following code i sometimes (not everytime) get the runtime error 9 that the index is out of range (hopefully i translated it correctly into english). 在以下代码中,有时(并非每次)我都会得到运行时错误9,表明索引超出范围(希望我将其正确翻译为英语)。 I am new to vba. 我是vba的新手。 I know that there are several similar problems already asked but i have a huge problem to adapt the solutions to my code as i don't really understand either the problem in my code as also the solutions of their problems. 我知道已经问过几个类似的问题,但是我有一个很大的问题要使解决方案适应我的代码,因为我既不真正理解我的代码中的问题,也不真正理解它们的问题。

I marked the line for which the error occurs with stars. 我用星号标记了发生错误的行。

I would be really thankful if anybody could explain, why this problem occurs in my code sometimes and how to solve it properly. 如果有人能解释,我会非常感激,为什么有时在我的代码中出现此问题,以及如何正确解决它。 Thank you in advance. 先感谢您。

Here's the code: 这是代码:

Sub calcinull()
Dim ione(4), itwo(4), ii, ints(4), cs(4), io, it As Double
Dim a, b, c As Double

ione(0) = 0
ione(1) = 10
ione(2) = 20
ione(3) = 30
ione(4) = 40

itwo(0) = 100
itwo(1) = 90
itwo(2) = 80
itwo(3) = 70
itwo(4) = 60


For b = 0 To 4
    ii = ione(b) + (((itwo(b) - ione(b)) * (NPV(ione(b))) / (NPV(ione(b)) - NPV(itwo(b)))))
    ints(b) = ii
    cs(b) = NPV(ii)
Next b

Dim AbsInt(4), AbsCs(4) As Double

For a = 0 To 4
    AbsInt(a) = VBA.Abs(ints(a))
    AbsCs(a) = VBA.Abs(cs(a))
Next a

Dim pos As Integer

pos = Application.Match(Application.Min(AbsCs), AbsCs, 0)

*ii = ints(pos)*

If NPV(ii) > 0 Then
    io = ii
    If pos > 0 Then
        it = itwo(pos - 1)
    Else
        it = itwo(0)
    End If
ElseIf NPV(ii) < 0 Then
    it = ii
    If pos > 0 Then
        io = ione(pos - 1)
    Else
        io = ione(0)
    End If
ElseIf NPV(ii) = 0 Then
    inull = ii
End If

For c = 1 To 30
    Do Until (NPV(io) - NPV(it)) <> 0
        io = io - 0.1
        it = it + 0.1
    Loop
        ii = io + (((it - io) * (NPV(io)) / (NPV(io) - NPV(it))))
        If NPV(ii) > 0 Then
            io = ii
            If it > (io + 0.5) Then
                it = it - 0.5
            End If
        ElseIf NPV(ii) < 0 Then
            it = ii
            If io < (it - 0.5) Then
                io = io + 0.5
            End If
        ElseIf NPV(ii) = 0 Then
            inull = ii
            Exit For
        End If
Next c
inull = ii

End Sub

As ints is an array with 5 elements (0..4), probably pos is > 4 when this error occurs. 由于ints是一个包含5个元素(0..4)的数组,因此发生此错误时pos可能> 4。

If you can't tell why, maybe put something like this behind the Match -Statement and set a breakpoint to the print while testing. 如果您不知道为什么,可以在Match -Statement后面放置类似的内容,并在测试时为print设置断点。

if pos < 0 or pos > 4 then
    debug.print pos & " is off"
end if

Alright guys, i solved it. 好的,我解决了。 The problem was, that the arrays uses indices from 0 to x, whereas the position gives the nth position of the array, which means, that my "pos"-variable is always one integer above the array-index. 问题是,数组使用从0到x的索引,而该位置给出了数组的第n个位置,这意味着我的“ pos”变量始终比array-index高1个整数。

Thank you all for your help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM