VBA for Access - Do While循环停止递减

时间:2016-09-08 13:58:06

标签: vba access-vba

我有一个无限的Do ...在VBA for Access中循环,我无法弄清楚发生了什么。我的代码类似于以下内容,其中我递减一个变量,当该变量小于或等于0时,它应该退出while循环。

Sub MySubRoutine (MyArray() as Double)
    Dim t as Double
    Dim s as Double    

    t = MyArray(0)
    s = t / 50 

    Do While (t>=0)

        'Various things that don't change t or s    

        t = t - s
        Debug.Print t; " "; s; " "; t - s
    Loop
End Sub

我的Debug.Print语句显示它正常减少了一段时间,然后它就停止了。以下是此循环的立即窗口输出的开头和结尾:

  1188 29.7 1158.3
   1158.3 29.7 1128.6
   1128.6 29.7 1098.9
   1098.9 29.7 1069.2
   1069.2 29.7 1039.5
   1039.5 29.7 1009.8
   1009.8 29.7 980.1
   ...
   ...
   207.9 29.7 178.2
   178.2 29.7 148.5
   176.3 29.7 146.6
   176.3 29.7 146.6
   176.3 29.7 146.6

如果我让它继续运行,那么最后3行只是无限重复。所以在代码t = t - s中工作了很长一段时间,然后好像s变成0.0,即使我的输出显示它仍然是29.7。它还表明,在Debug.Print行中,t - s被正确评估。

我也尝试将t = t - s更改为:

temp = t - s
t = temp

但这没有帮助。

有关可能发生的事情的任何线索?

1 个答案:

答案 0 :(得分:0)

您的代码缺少某些内容,而且您的结果无法重现。

使用值1212.24489795918调用(为了获得t = 1188的第一次计算),循环终止,结果为(逗号是我的小数点分隔符):

 1188   24,2448979591837   1163,75510204082 
 1163,75510204082   24,2448979591837   1139,51020408163 
 1139,51020408163   24,2448979591837   1115,26530612245 
 1115,26530612245   24,2448979591837   1091,02040816327 
 1091,02040816327   24,2448979591837   1066,77551020408 
 1066,77551020408   24,2448979591837   1042,5306122449 
 1042,5306122449   24,2448979591837   1018,28571428571 
 1018,28571428571   24,2448979591837   994,040816326531 
 994,040816326531   24,2448979591837   969,795918367347 
 969,795918367347   24,2448979591837   945,551020408164 
 945,551020408164   24,2448979591837   921,30612244898 
 921,30612244898   24,2448979591837   897,061224489797 
 897,061224489797   24,2448979591837   872,816326530613 
 872,816326530613   24,2448979591837   848,571428571429 
 848,571428571429   24,2448979591837   824,326530612246 
 824,326530612246   24,2448979591837   800,081632653062 
 800,081632653062   24,2448979591837   775,836734693879 
 775,836734693879   24,2448979591837   751,591836734695 
 751,591836734695   24,2448979591837   727,346938775511 
 727,346938775511   24,2448979591837   703,102040816328 
 703,102040816328   24,2448979591837   678,857142857144 
 678,857142857144   24,2448979591837   654,61224489796 
 654,61224489796   24,2448979591837   630,367346938777 
 630,367346938777   24,2448979591837   606,122448979593 
 606,122448979593   24,2448979591837   581,87755102041 
 581,87755102041   24,2448979591837   557,632653061226 
 557,632653061226   24,2448979591837   533,387755102042 
 533,387755102042   24,2448979591837   509,142857142859 
 509,142857142859   24,2448979591837   484,897959183675 
 484,897959183675   24,2448979591837   460,653061224491 
 460,653061224491   24,2448979591837   436,408163265308 
 436,408163265308   24,2448979591837   412,163265306124 
 412,163265306124   24,2448979591837   387,91836734694 
 387,91836734694   24,2448979591837   363,673469387757 
 363,673469387757   24,2448979591837   339,428571428573 
 339,428571428573   24,2448979591837   315,183673469389 
 315,183673469389   24,2448979591837   290,938775510206 
 290,938775510206   24,2448979591837   266,693877551022 
 266,693877551022   24,2448979591837   242,448979591838 
 242,448979591838   24,2448979591837   218,204081632655 
 218,204081632655   24,2448979591837   193,959183673471 
 193,959183673471   24,2448979591837   169,714285714287 
 169,714285714287   24,2448979591837   145,469387755104 
 145,469387755104   24,2448979591837   121,22448979592 
 121,22448979592   24,2448979591837   96,9795918367362 
 96,9795918367362   24,2448979591837   72,7346938775525 
 72,7346938775525   24,2448979591837   48,4897959183688 
 48,4897959183688   24,2448979591837   24,2448979591852 
 24,2448979591852   24,2448979591837   1,48503431773861E-12 
 1,48503431773861E-12   24,2448979591837  -24,2448979591822 
-24,2448979591822   24,2448979591837  -48,4897959183659 

但是s远远超过你的29.7值。

所以在mitch看起来有点意思。