2014-03-18 3 views
0

누군가가 내 Excel이 충돌하는 이유에 대한 통찰력을 제공 할 수 있습니까? 임씨는 VBA를 배우려고하고 조언이 필요합니다.카운트 다운 타이머 Excel VBA - 코드 추락 Excel

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 
+0

초기 값 ** A1 **의 무엇입니까? ** A1 ** 및 ** D2 **는 어떻게 관련이 있습니까 ?? –

+0

OMG im twit .... –

+0

A1 = 00:00:30, D2는 A1이어야합니다 ..... 또한 대기 시간을 Application.Wait Now + TimeValue ("0:00:01")로 변경했습니다. –

답변

1

나는 우주 왕복선 발사 또는 그와 같은 중요한 것을 타이밍을 맞추지 않을 것이라고 확신합니다. 그러나 카운트 다운이 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 
+0

당신이 거시기 고맙다, 이건 정말 도움이되었습니다. 이것은이 카운트 다운 타이머를 만드는 훨씬 좋은 방법입니다. –

0

이렇게하는 것이 가장 좋은 방법입니다.

아래에이 코드를 사용해보십시오 :

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