2016-11-28 77 views
0

我正在請求幫助,爲什麼省略了以下代碼的最後一個外部循環。此代碼是醫療保健模擬的一部分,該模擬使用VBA遍歷參數組合以生成敏感性分析。我有3個其他敏感性分析沒有問題。值得注意的是,子call_transplant_surv是一個高度保守的程序,在許多其他未在此處顯示的操作中沒有問題地運行。我試圖撇取代碼來隔離問題而沒有成功。我沒有注意到在某些值爲txp1b時會導致故障的表單上的錯誤。VBA省略最後一個外部循環

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 

回答

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 
+0

也可以使用[十進制](https://msdn.microsoft.com/en-us/library/office/gg251687.aspx)或[貨幣](HTTPS: //msdn.microsoft.com/en-us/library/of fice/gg264338.aspx)使用整數作爲基本類型並且不受浮點錯誤傳播影響的數據類型。 –

+0

@VincentG - 這也適用於這種情況(我並不完全清楚在編譯時是否隱式投射,以及在哪裏),但請記住,計數器本身將被視爲縮放類型,但任何文字需求將[hinted](http://stackoverflow.com/documentation/vba/877/declaring-variables/2960/type-hints#t=201611281711181796862)轉換爲正確的類型(或顯式聲明爲縮放變量)。 I.e .:「因爲I = 0.05 @至0.3 @步0.05」。在OP的代碼中,它隱含地(未聲明的)「Variant」,所以它被視爲「Double」。 – Comintern