我希望我的代码遍历工作表,搜索存在正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
答案 0 :(得分:1)
如果要退出Exit For
循环
End If
语句前加For
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