VBA省略了最后一个外部循环 [英] VBA omitting last outer loop

查看:139
本文介绍了VBA省略了最后一个外部循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在请求帮助,为什么省略了以下代码的最后一个外部循环。此代码是医疗保健模拟的一部分,该模拟使用VBA遍历参数组合以生成敏感性分析。我有3个其他敏感性分析没有问题。值得注意的是,sub call_transplant_surv 是一个高度保守的程序,在许多其他的操作中没有显示在这里。我试图镂空代码来隔离问题没有成功。我没有注意到表格上的错误,会导致 txp1b 的某些值失败。

  Sub twoway1()

列表中的延迟和1B VAD txp费率

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

Dim i As Long,j As Long,counter As Long
Dim_Bit_by 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


'开始时间计数器
timestart =时间

'设定值
prob_bin = 0'概率模型= 1
delay_list = 0'开始于30给定循环
status_2_bin = 0'正常值= 0
elective_days = 30'允许的固定值1A天
first_da y = 30'第一天的选修时间被使用,在宏w / oa变量中增加
posttxp_death = 1
twoway1 = 1
twoway2 = 0
txp1b = 0
delay_i = 0

time_measure = 0'测量​​时间(例如,在0天所有参数被测量,30天全部被测量,等等)
timemeas_inc = 30'测量时间的增量(例如每30天 - 30,60,90,...

计数器= 1

将设置输入到模型
表格(设置)。范围(C27)。值= prob_bin
表格(设置) 。Range(C28)。Value = delay_list
表格(设置)。范围(C29)。值= status_2_bin
表格(设置)。范围(C30)。值= elective_days
表格(设置)。范围(C31)。值= first_day
表格(设置)。范围(C32)。值= posttxp_death
表格(设置)。Range(C44)。Value = twoway1
Sheets(settings)。Range(C45)。Value = twoway2

calculate

'输入两个循环来控制参数
'输入两个循环来控制参数
对于txp1b = 0 .05至0.3步骤0.05
对于delay_i = 0至360步骤90

表格(设置)。范围(C31)。值= delay_i + 30
表格(设置)范围(C28)值=延迟_i
表格(1B> TXP Weib)。范围(J20)。值= txp1b

计算

'移植生存计算
call_txp_surv

'输入测量循环
对于i = 1至61

'
表格(settings)。Range(AD4)。Value = time_measure

'加速计算第二部分
计算

'记录模拟结果转换成图表delay_list行/列
表格(twoway1)。激活
表格(twoway1)。Range(Cells(计数器+1,1),单元格(计数器+ 1,45))。值=表(设置)。范围(M4:BE4)。值

'递增时间点数据记录
time_measure = time_measure + timemeas_inc

'递增计数器正确放置下一个循环的结果
counter = counter + 1

Next i

time_measure = 0

下一个
下一个

time_all =时间 - 时间开始
'表格(twoway1)。范围(AU2)Value = time_all

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

End Sub


解决方案

正在使用一个非整数循环计数器 - 我的猜测是,由于浮点错误,循环提前退出:

pre $私人小例子()
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


I am requesting assistance as to why the last outer loop of the following code is omitted. This code is part of a healthcare simulation, which uses VBA to iterate through combinations of parameters to generate sensitivity analyses. I have 3 other sensitivity analyses operating without issue. Notably, the sub call_transplant_surv is a highly conserved program that operates without issue in many other operatiions not shown here. I have tried skeletonizing the code to isolate the issue without success. I have not noted an error on the sheets that would cause failure at certain values of 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

解决方案

The issue is using a non-integer loop counter - my guess is that the loop is exiting early because of a floating point error:

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

My recommendation would be to use integer iterations and then calculate the working value separately:

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

这篇关于VBA省略了最后一个外部循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆