简体   繁体   English

在Excel VBA中创建毫秒循环

[英]Create Millisecond Loops in Excel VBA

Since I just found out about Excel Macros, I want to try to simulate moving objects. 由于我刚刚了解Excel宏,因此我想尝试模拟移动对象。 I would like to run some looped code every 'frame' of my project. 我想在项目的每个“框架”中运行一些循环代码。 I can make an infinite loop in Excel VBA with this code: 我可以使用以下代码在Excel VBA中进行无限循环:

Do While True:
    'code
Loop

However, this crashes Excel. 但是,这会使Excel崩溃。 Is there a way to make an infinite loop that runs every ten milliseconds or so, something like this: 有没有一种方法可以使每十毫秒左右运行一次的无限循环,如下所示:

Dim timer as Timer
If timer = 10 Then
    'code
    timer = 0
End If

EDIT: Your answers are very good, but not exactly what I'm looking for. 编辑:您的答案很好,但不完全是我想要的。 I want to be able to run other code at the same time; 我希望能够同时运行其他代码; a bit like Javascript's 有点像Javascript的

setInterval(function(){}, 200);

which can run multiple functions simultaneously. 可以同时运行多个功能。

You can use an API call and Sleep. 您可以使用API​​调用和睡眠。

Put this at the top of your module: 将其放在模块顶部:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then you can call it in a procedure like this: 然后,您可以按以下过程调用它:

Do While True:

Sleep 10    'wait 0.01 seconds

Loop

If the code is in 64bit OS, you will need to use PtrSafe. 如果代码是在64位OS中,则需要使用PtrSafe。 See https://support.microsoft.com/en-us/help/983043/compile-error-the-code-in-this-project-must-be-updated-for-use-on-64 请参阅https://support.microsoft.com/zh-cn/help/983043/compile-error-the-code-in-this-project-must-be-updated-for-use-on-64

Your original method is crashing Excel because it is creating an infinite loop with no exit condition. 您的原始方法使Excel崩溃,因为它正在创建没有退出条件的无限循环。

The second method doesn't work because your system clock time (given by Timer ) will never be 10 , if you use debug.Print(Timer) in your Immediate Window you will see its value. 第二种方法不起作用,因为您的系统时钟时间(由Timer给出)永远不会为10 ,如果您在“即时窗口”中使用debug.Print(Timer) ,则会看到其值。

Here is some commented code to execute some actions based on a timer. 这是一些注释代码,用于基于计时器执行某些操作。 Please please PLEASE make sure you retain the runtime condition for exiting the while loop, infinite loops are the devil and really you should have some other exit code in here somewhere! 请请确保您保留退出while循环的runtime条件,无限循环是魔鬼,实际上您应该在此处的某个位置有其他退出代码!

Sub timeloop()
    Dim start As Double: start = Timer     ' Use Timer to get current time
    Dim t As Double: t = start             ' Set current time "t" equal to start
    Dim interval As Double: interval = 1   ' Interval for loop update (seconds)
    Dim nIntervals As Long: nIntervals = 0 ' Number of intervals passed

    ' Use this While loop to avoid an infinite duration! Only runs for "runtime" seconds
    Dim runtime As Double: runtime = 10
    Do While t < start + runtime
        ' Check if a full interval has passed
        If (t - start) / interval > nIntervals Then
            nIntervals = nIntervals + 1
            ' Do stuff here ---
            Debug.Print (t)
            ' -----------------
        End If
        t = Timer ' Update current time
    Loop
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM