在满足条件的情况下结束while循环

时间:2013-09-16 16:26:18

标签: vba loops while-loop

我正在将Mathcad代码转换为VBA,并试图弄清楚如何复制While循环,该循环要求程序在TGuess< 0.在循环结束时是一个if语句,如果sGuess> 1 / 1.4(我会附上一张图片,但我的名字不允许我这样做),就会打破循环。

我已经在VBA中编写了这段代码,但是我想知道在原始的While语句中包含sGuess变量是否正确,或者它是否会影响循环的输出:

    While TGuess < 0 And sGuess <= 1 / 1.4

        kterm = (kj ^ (1 / 6)) / 26 'k term in the numerator of depth equation
                    epw = 3 / 5
        FDepth = ((kterm * RainInt * L * CF) / sGuess ^ 0.5) ^ epw

        tflow = UW_Wat * g * sGuess * FDepth    'Calc Flow Shear Stress

        pflow = 7.853 * UW_Wat * (tflow / UW_Wat) ^ (3 / 2)

        TGuess = pflow - pcrit   'Recalc TGuess as E-P
        sGuess = sGuess + SlopeInc  'Calc new stable slope
    Wend

任何意见都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

为了减轻您的顾虑,可能最好用Do While ... Loop块替换while ... wend循环。然后,您可以使用

中的内容将您的中断条件放在相应的Mathcad代码中。
If sGuess > 1/1.4 Then
    Exit Do
End If