倒数计时器 Excel VBA - 代码崩溃 excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22489755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Countdown Timer Excel VBA - Code crashed excel
提问by Lewis Morris
Could someone offer an insight into why this crashes my excel, i cant seem to work it out. Im trying to learn VBA and need some advice.
有人可以深入了解为什么这会导致我的 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
回答by Dick Kusleika
I'm sure you're not timing space shuttle launches or anything that critical. But if you want to make sure your countdown doesn't take longer than 30 seconds, you can use the Timer function. Here's an example.
我敢肯定你不是在为航天飞机发射或任何重要的事情计时。但如果您想确保倒计时不超过 30 秒,则可以使用 Timer 功能。这是一个例子。
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
回答by KumaraPush
If it just about count down every second, I guess there is another option to add a Timer functionality to Excel. Try searching for Application.OnTime function in Excel that enables us to have Timer in Excel. Related Article that explains how to add timer in Excel and make a macro run during that scheduled interval.
如果它只是每秒倒计时,我想还有另一种选择可以向 Excel 添加计时器功能。尝试在 Excel 中搜索 Application.OnTime 函数,它使我们能够在 Excel 中使用计时器。相关文章解释了如何在 Excel 中添加计时器并在预定的时间间隔内运行宏。
回答by RomeuForte
This is the best way to do this.
这是最好的方法。
Try to use this code below:
尝试使用以下代码:
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