Hoe pauzeren voor een bepaalde tijd? (Excel/VBA)

Ik heb een Excel-werkblad met de volgende macro. Ik zou het elke seconde willen herhalen, maar ik zou de functie vinden om dat te doen. Is het niet mogelijk?

Sub Macro1()
'
' Macro1 Macro
'
Do
    Calculate
    'Here I want to wait for one second
Loop
End Sub

Antwoord 1, autoriteit 100%

Gebruik de Wachtmethode:

Application.Wait Now + #0:00:01#

of (voor Excel 2010 en later):

Application.Wait Now + #12:00:01 AM#

Antwoord 2, autoriteit 48%

Voeg dit toe aan je module

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

Of gebruik voor 64-bits systemen:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Noem het als volgt in je macro:

Sub Macro1()
'
' Macro1 Macro
'
Do
    Calculate
    Sleep (1000) ' delay 1 second
Loop
End Sub

Antwoord 3, autoriteit 32%

in plaats van:

Application.Wait(Now + #0:00:01#)

ik geef de voorkeur aan:

Application.Wait(Now + TimeValue("00:00:01"))

omdat het achteraf een stuk makkelijker te lezen is.


4, Autoriteit 16%

Dit werkt perfect voor mij. Plaats een code voor of na de “Do tot” -lus. Plaats in uw geval de 5 regels (time1 = & amp; time2 = & amp; “doen tot” lus) aan het einde in uw do-lus

sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
    Do Until time1 >= time2
        DoEvents
        time1 = Now()
    Loop
End sub

5, Autoriteit 9%

De verklaring voor de slaap in kernel32.dll zal niet werken in 64-bit Excel. Dit zou een beetje meer algemeen zijn:

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

6, Autoriteit 4%

Gewoon een opgeruimde versie van Clemo’s Code – Werkt in Access, wat de functie van de toepassing niet heeft.

Public Sub Pause(sngSecs As Single)
    Dim sngEnd As Single
    sngEnd = Timer + sngSecs
    While Timer < sngEnd
        DoEvents
    Wend
End Sub
Public Sub TestPause()
    Pause 1
    MsgBox "done"
End Sub

7, Autoriteit 2%

Application.Wait Second(Now) + 1


8

Function Delay(ByVal T As Integer)
    'Function can be used to introduce a delay of up to 99 seconds
    'Call Function ex:  Delay 2 {introduces a 2 second delay before execution of code resumes}
        strT = Mid((100 + T), 2, 2)
            strSecsDelay = "00:00:" & strT
    Application.Wait (Now + TimeValue(strSecsDelay))
End Function

9

Hier is een alternatief voor de slaap:

Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub

In de volgende code maak ik een “GLOW” -effect Blink op een draaiknop om gebruikers naar toe te sturen als ze “problemen hebben”, met behulp van “Sleep 1000” in de lus resulteerde in geen zichtbare knipperen, maar de lus is goed werken.

Sub SpinFocus()
Dim i As Long
For i = 1 To 3   '3 blinks
Worksheets(2).Shapes("SpinGlow").ZOrder (msoBringToFront)
TDelay (10000)   'this makes the glow stay lit longer than not, looks nice.
Worksheets(2).Shapes("SpinGlow").ZOrder (msoSendBackward)
TDelay (100)
Next i
End Sub

10

Wacht- en slaapfuncties vergrendelen Excel en u kunt niets anders doen totdat de vertraging is voltooid. Aan de andere hand geeft Loop vertragingen u geen exacte tijd om te wachten.

Dus ik heb deze oplossing gemaakt met een beetje van beide concepten. Het loopt tot de tijd de tijd die je wilt.

Private Sub Waste10Sec()
   target = (Now + TimeValue("0:00:10"))
   Do
       DoEvents 'keeps excel running other stuff
   Loop Until Now >= target
End Sub

U hoeft alleen maar afval10SEC te bellen waar u de vertraging nodig hebt


11

Ik had dit gemaakt om het probleem te beantwoorden:

Sub goTIMER(NumOfSeconds As Long) 'in (seconds) as:  call gotimer (1)  'seconds
  Application.Wait now + NumOfSeconds / 86400#
  'Application.Wait (Now + TimeValue("0:00:05"))  'other
  Application.EnableEvents = True       'EVENTS
End Sub

12

Probeer dit:

Threading.thread.sleep(1000)

Antwoord 13

U kunt gebruiken
Application.wait now + timevalue(“00:00:01”)
of
Application.wait now + timeserial(0,0,1)

Other episodes