Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba N次迭代后屏幕未更新_Vba_Excel_While Loop_Crash - Fatal编程技术网

Vba N次迭代后屏幕未更新

Vba N次迭代后屏幕未更新,vba,excel,while-loop,crash,Vba,Excel,While Loop,Crash,我在我的excel上运行一个非常简单的“蛇形”游戏,这意味着我需要在主while循环中不断更新屏幕。“蛇”(基本上是彩色单元格)在20x20表格中随机移动 它在几秒钟内运行良好,但某些点屏幕停止更新;ans excel停止响应,同时仍在运行代码。这导致了一场车祸 我将while循环替换为For循环,这样可以避免崩溃,但屏幕仍会停止更新,直到循环结束。循环停止时,屏幕将最后更新一次。 我还在每个函数后面都包含了“Sleep”语句,但是,这没有帮助。 以下是主要代码: Sub main()

我在我的excel上运行一个非常简单的“蛇形”游戏,这意味着我需要在主while循环中不断更新屏幕。“蛇”(基本上是彩色单元格)在20x20表格中随机移动

它在几秒钟内运行良好,但某些点屏幕停止更新;ans excel停止响应,同时仍在运行代码。这导致了一场车祸

我将while循环替换为For循环,这样可以避免崩溃,但屏幕仍会停止更新,直到循环结束。循环停止时,屏幕将最后更新一次。 我还在每个函数后面都包含了“Sleep”语句,但是,这没有帮助。 以下是主要代码:

    Sub main()
    Dim table(20, 20) As Integer
    Dim index_move As Integer
    'Position class contains only X and Y parameters
    Dim index_actual_head_pos As Position 
    Set index_actual_head_pos = New Position

    Dim i As Long
    index_actual_head_pos.x = 5
    index_actual_head_pos.y = 15
    Application.ScreenUpdating = True

    For i = 1 To 50 'this remplaces the initial while loop
    'next line generates a random movement, bounded by a 20x20 matrix
    index_move = generer_movement(index_actual_head_pos)
    'next line updates the "snake" position params
    Call update_position(index_actual_head_pos, index_move)
    'next line deletes previous snake and draws a new one
    Call draw(index_actual_head_pos)
    Sleep (200)
    Next i
    End Sub
请注意,问题实际上是屏幕更新。如前所述,我不知道如何强制excel不断更新屏幕

非常感谢,


Denis

您可能应该放弃睡眠(200),而是让Excel应用程序对象在200毫秒内进行一些处理

dim dTill as double   'should be at the top with the other var declarations

dtill = Timer + 0.200
do while timer < dTill and timer > 0.200: DoEvents: loop
dim dTill as double'应与其他var声明一起位于顶部
dtill=计时器+0.200
当定时器0.200:DoEvents:loop时执行

计时器>0.200
非常重要,因为计时器在午夜重置,如果您在那一刻跨过午夜,您不会希望它将所有东西都暂停24小时₅ 其次。

非常感谢,我不知道“计时器”和DoEvents的用法。这很有效。