0

I want my code to go through the sheet, searching for the point where there is a positive d value followed by a negative d value, then execute the next equation which is CCT.

It won't recognize that the IF statement actually becomes true at i=22, and instead will go through the whole loop and give me a division by zero error.

Any help is appreciated.

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

1 Answer 1

1

Put an Exit For right before your End If statement if you want to exit the For loop

 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.