VBA省略了最后一个外循环

时间:2016-11-28 16:11:48

标签: vba for-loop

我正在请求帮助,以解释为什么省略下面代码的最后一个外部循环。此代码是医疗保健模拟的一部分,该模拟使用VBA迭代参数组合以生成灵敏度分析。我有3个其他灵敏度分析没有问题。值得注意的是,子call_transplant_surv是一个高度保守的程序,在此处未示出的许多其他操作中没有问题。我试过篡改代码以隔离问题但没有成功。我没有注意到工作表上的错误会导致txp1b的某些值失败。

Sub twoway1()

        'delay in list and 1B VAD txp rate

        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Application.EnableEvents = False
        Application.DisplayStatusBar = False

        Dim i As Long, j As Long, counter As Long
        Dim prob_bin As Byte, delay_list As Byte, status_2_bin As Byte, elective_days As Byte, first_day As Byte
        Dim timestart As Double, timeall As Double, twoway1 As Integer, twoway2 As Integer, delay_i As Integer


        'begin time counter
        timestart = Time

        'set values
        prob_bin = 0            'probabilistic model = 1
        delay_list = 0          'set to  to begin at 30 given loop
        status_2_bin = 0        'normal values = 0
        elective_days = 30      'fixed value of 1A days allowed
        first_day = 30          'first day elective time is used, incremented in the macro w/o a variable
        posttxp_death = 1
        twoway1 = 1
        twoway2 = 0
        txp1b = 0
        delay_i = 0

        time_measure = 0       'measurement time (e.g. at 0 days all parameters are measured, 30 days all measured, etc.)
        timemeas_inc = 30      'increment of the measurement time (e.g. every 30 days- 30, 60, 90,....

        counter = 1

            'enter settings into model
            Sheets("settings").Range("C27").Value = prob_bin
            Sheets("settings").Range("C28").Value = delay_list
            Sheets("settings").Range("C29").Value = status_2_bin
            Sheets("settings").Range("C30").Value = elective_days
            Sheets("settings").Range("C31").Value = first_day
            Sheets("settings").Range("C32").Value = posttxp_death
            Sheets("settings").Range("C44").Value = twoway1
            Sheets("settings").Range("C45").Value = twoway2

            calculate

            'enter two loops to control the parameters
            'enter two loops to control the parameters
                For txp1b = 0.05 To 0.3 Step 0.05
                    For delay_i = 0 To 360 Step 90

                    Sheets("settings").Range("C31").Value = delay_i + 30
                    Sheets("settings").Range("C28").Value = delay_i
                    Sheets("1B>TXP Weib").Range("J20").Value = txp1b

                    calculate

                    'transplant survival calcs
                    call_txp_surv

                    'enter measurement loop
                    For i = 1 To 61

                        'place time measured
                        Sheets("settings").Range("AD4").Value = time_measure

                        'speed up calcs part 2
                        calculate

                        'record simulation results into sheet delay_list Row/column
                        Sheets("twoway1").Activate
                        Sheets("twoway1").Range(Cells(counter + 1, 1), Cells(counter + 1, 45)).Value = Sheets("settings").Range("M4:BE4").Value

                        'increment the time point for data recording
                        time_measure = time_measure + timemeas_inc

                        'increment counter for correct placement of next loop of results
                        counter = counter + 1

                    Next i

                time_measure = 0

                Next
            Next

            time_all = Time - timestart
            'Sheets("twoway1").Range("AU2").Value = time_all

            Application.Calculation = xlCalculationAutomatic
            Application.ScreenUpdating = True
            Application.EnableEvents = True
            Application.DisplayStatusBar = True

    End Sub

1 个答案:

答案 0 :(得分:2)

问题是使用非整数循环计数器 - 我的猜测是循环因为浮点错误而提前退出:

Private Sub Example()
    Dim i As Double
    For i = 0.05 To 0.3 Step 0.05
        Debug.Print i
    Next
End Sub

我的建议是使用整数迭代,然后分别计算工作值:

Dim i As Long
For i = 1 To 6
    txp1b = i * 0.05
    '...
Next