倒计时计时器Excel VBA-代码崩溃Excel

倒计时计时器Excel VBA-代码崩溃Excel,excel,vba,timer,crash,countdown,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 -

如果有人能解释一下为什么我的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中搜索Application.OnTime函数,该函数使我们能够在Excel中设置计时器

这是最好的方法

请尝试使用以下代码:

Sub Countup() 
Dim CountDown As Date 
CountDown = Now + TimeValue("00:00:01") 
Application.OnTime CountDown, "Realcount" 
End Sub

Sub Realcount() 'Run this to count down your cell value
Dim count As Range 
Set count = [E1] 'Put the range that you want to count down
count.Value = count.Value - TimeSerial(0, 0, 1) 
If count <= 0 Then 
MsgBox "Countdown complete." 
Exit Sub 
End If 
Call Countup 
End Sub
Sub Countup()
日期倒数
倒计时=现在+时间值(“00:00:01”)
Application.OnTime倒计时,“Realcount”
端接头
Sub Realcount()'运行此命令以倒计时单元格值
变暗计数为范围
Set count=[E1]'设置要倒计时的范围
count.Value=count.Value-TimeSerial(0,0,1)

如果计算A1的初始值是多少??A1和D2有什么关系?天哪,我真是个傻瓜……A1=00:00:30,D2应该是A1。。。。。我还更改了wait to应用程序。wait Now+TimeValue(“0:00:01”)谢谢迪克,这对我真的很有帮助。这似乎是制作倒计时计时器的更好方法。