excel - Excel VBA倒数计时器-代码崩溃的Excel

标签 excel vba timer crash countdown

有人能提供一个为什么这会导致我的excel崩溃的见解,我似乎无法解决。
我正在尝试学习VBA,需要一些建议。

sub timer()

    dim second

    second = 1.15740740740741e-05 

'this is the amount excel counts as a second

line1:

    application.wait "00:00:01"

    Range("a1").value = Range("a1").value - second

    if not range("D2").value = 0 then

        Goto line1

            else

            Msgbox("Countdown ended")

    End if

end sub

最佳答案

我确定您没有安排航天飞机的发射时间或任何关键的时间。但是,如果要确保倒计时不超过30秒,可以使用计时器功能。这是一个例子。

Sub NewTimer()

    Dim Start As Single
    Dim Cell As Range
    Dim CountDown As Date

    'Timer is the number of seconds since midnight.
    'Store timer at this point in a variable
    Start = Timer

    'Store A1 in a variable to make it easier to refer
    'to it later. Also, if the cell changes, you only
    'have to change it in one place
    Set Cell = Sheet1.Range("A1")

    'This is the starting value. Timeserial is a good
    'way to get a time
    CountDown = TimeSerial(0, 0, 30)

    'Set our cell to the starting value
    Cell.Value = CountDown

    'Keep executing this loop until A1 hits zero or
    'even falls slightly below zero
    Do While Cell.Value > 0
        'Update the cell. Timer - Start is the number of seconds
        'that have elapsed since we set Start.
        Cell.Value = CountDown - TimeSerial(0, 0, Timer - Start)

        'DoEvents release control ever so briefly to Windows. This
        'allows Windows to do stuff like update the screen. When you
        'have loops like this, your code appears frozen because it's
        'not letting Windows do anything (unless you have this line)
        DoEvents
    Loop

End Sub

关于excel - Excel VBA倒数计时器-代码崩溃的Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489755/

相关文章:

用于检查文本的 Excel 公式

r - 如何导出 R 随机森林模型以在 Excel VBA 中使用而无需 API 调用

vba - 改进 PasteValue 宏以使用 Cut

VBA 函数返回#VALUE!在工作表中,作为子工作

jsf - ManagedBean 中的远程轮询并通过推送通知客户端 View

c# - 如何在 for 循环中等待 x 秒 C# - unity

excel - 使用 VBA 以最小延迟动态隐藏/取消隐藏多个范围

excel - 查找字符串中包含子字符串的所有行,并返回 ms-excel 中所有此类行的第一个单元格

vba - excel函数语法调用错误

c# - 如何使用计时器替换Azure辅助角色中的Thread.Sleep(...)?