0

我希望我的代码遍历工作表,搜索有一个正 d 值后跟一个负 d 值的点,然后执行下一个等式,即 CCT。

它不会识别 IF 语句实际上在 处变为真i=22,而是会遍历整个循环并给我一个除以零的错误。

任何帮助表示赞赏。

Sub CCT()

'Variable Declarations

Dim us As Double        'u coordinate of source
Dim vs As Double        'v coordinate of source
Dim u(100) As Double     'u coordinate of the isotemperature line
Dim v(100) As Double     'v coordinate of the isotemperature line
Dim d(100) As Double     'Distance between the source line and the isotemperature line
Dim t(100) As Double     'Slope of the isotemperature line
Dim tt(100) As Double    'Temperature
Dim i As Integer        'Counter
Dim d1 As Double        'dj
Dim d2 As Double        'dj+1
Dim tt1 As Double       'tj
Dim tt2 As Double       'tj+1
Dim CCT As Double       'CCT

'Reading in the Variables

us = Worksheets("CCTIsotemp").Cells(10, 12).Value
vs = Worksheets("CCTIsotemp").Cells(10, 13).Value

'Doing the Math

    For i = 1 To 31
        u(i) = Worksheets("CCTIsotemp").Cells(9 + i, 4).Value
        v(i) = Worksheets("CCTIsotemp").Cells(9 + i, 5).Value
        t(i) = Worksheets("CCTIsotemp").Cells(9 + i, 6).Value
        tt(i) = Worksheets("CCTIsotemp").Cells(9 + i, 3).Value

        d(i) = ((vs - v(i)) - t(i) * (us - u(i))) / (1 + t(i) ^ 2) ^ 1 / 2



    If d(i) < 0 And d(i - 1) > 0 Then

        d1 = d(i - 1)
        d2 = d(i)
        tt1 = t(i - 1)
        tt2 = t(i)


    End If
    Next i

    CCT = ((1 / tt1) + (d1 / (d1 - d2)) * ((1 / tt2) - (1 / tt1))) ^ (-1)



    'write back

        Worksheets("CCTIsotemp").Cells(10, 15) = CCT
        Worksheets("CCTIsotemp").Cells(10, 16) = d1
        Worksheets("CCTIsotemp").Cells(10, 17) = d2
        Worksheets("CCTIsotemp").Cells(10, 18) = d(i)
        Worksheets("CCTIsotemp").Cells(10, 19) = i


End Sub
4

1 回答 1

1

如果你想退出循环,在你的语句之前放一个Exit For右边End IfFor

 If d(i) < 0 And d(i - 1) > 0 Then

        d1 = d(i - 1)
        d2 = d(i)
        tt1 = t(i - 1)
        tt2 = t(i)

Exit For
End If
于 2013-09-19T01:59:33.847 回答