循环不会停止,导致溢出

我是VBA的新手,这看起来很简单,但是当我运行代码时,它会继续创build随机数,直到它最终给我一个溢出消息。 我可以告诉它可以读总数,但我不知道为什么它不会停止。

Dim i As Integer Dim total As Integer i = 1 total = Range("C1") Do While total < 180 Cells(i, 1).Value = Int((10 - 3 + 1) * Rnd + 3) i = i + 1 Loop 

谢谢你的帮助。

我假设C1有一些types的总和或者是在VBA填充数字的时候增长,如果是这样的话,那么你只需要在每个循环结束时重新评估总数。 这是为我工作…

 Dim i As Integer Dim total As Integer i = 1 total = Range("C1") Do While total < 180 Cells(i, 1).Value = Int((10 - 3 + 1) * Rnd + 3) i = i + 1 Application.Calculate 'Only needed if manual calculation is on total = Range("C1") Loop 

我认为你的while循环使用了错误的variables。 你的意思是总计<180或我<180?