18

コードで 1 秒の遅延が必要です。以下は、この遅延を作ろうとしているコードです。オペレーティングシステムから日付と時刻をポーリングし、時刻が一致するまで待機していると思います。遅延に問題があります。待ち時間に一致する時間をポーリングせず、ただそこに座ってフリーズすると思います。コードを実行する時間の約 5% しかフリーズしません。Application.Wait と、ポーリングされた時間が待機時間よりも大きいかどうかを確認する方法があるかどうかについて疑問に思っていました。

   newHour = Hour(Now())
   newMinute = Minute(Now())
   newSecond = Second(Now()) + 1
   waitTime = TimeSerial(newHour, newMinute, newSecond)
   Application.Wait waitTime
4

13 に答える 13

38

Excel VBAを使用している場合は、次を使用できます。

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

(時間文字列はH:MM:SSのようになります。)

于 2011-09-26T01:00:42.520 に答える
25

この小さな関数を VBA に使用します。

Public Function Pause(NumberOfSeconds As Variant)
    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim Start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Elapsed = 0
    Do While Timer < Start + PauseTime
        Elapsed = Elapsed + 1
        If Timer = 0 Then
            ' Crossing midnight
            PauseTime = PauseTime - Elapsed
            Start = 0
            Elapsed = 0
        End If
        DoEvents
    Loop

Exit_GoTo:
    On Error GoTo 0
    Exit Function
Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function
于 2011-08-05T18:07:53.197 に答える
13

これをモジュールにコピーできます。

Sub WaitFor(NumOfSeconds As Long)
Dim SngSec as Long
SngSec=Timer + NumOfSeconds

Do while timer < sngsec
DoEvents
Loop

End sub

一時停止を適用するときはいつでも書き込み:

Call WaitFor(1)

お役に立てば幸いです。

于 2011-08-05T18:25:25.027 に答える
7

Sleepを使ってみましたか?

ここに例があります(以下にコピー):

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

Private Sub Form_Activate()    

frmSplash.Show
DoEvents
Sleep 1000
Unload Me
frmProfiles.Show

End Sub

選択した時間、アプリケーションがフリーズする可能性があることに注意してください。

于 2011-08-05T18:20:59.093 に答える
1

あなたのコードは日付のない時間を作成するだけです。アプリケーションを実行するときに、実際にその時間にすでに達した時間を待つという仮定が正しければ、正確に24時間待機します。また、now()を複数回呼び出すことについても少し心配しています(異なる可能性がありますか?)コードを次のように変更します

 application.wait DateAdd("s", 1, Now)
于 2011-08-05T18:24:32.087 に答える
1

タイマー機能は、Access 2007、Access 2010、Access 2013、Access 2016、Access 2007 Developer、Access 2010 Developer、Access 2013 Developer にも適用されます。このコードを挿入して、特定の秒数の時間を一時停止します

T0 = Timer
Do
    Delay = Timer - T0
Loop Until Delay = 1 'Change this value to pause time in second
于 2016-07-02T23:41:18.040 に答える
1

スティーブ・マロリーの回答の別のバリエーションです。待機中に逃げて何かをするために特にExcelが必要で、1秒が長すぎました。

'Wait for the specified number of milliseconds while processing the message pump
'This allows excel to catch up on background operations
Sub WaitFor(milliseconds As Single)

    Dim finish As Single
    Dim days As Integer

    'Timer is the number of seconds since midnight (as a single)
    finish = Timer + (milliseconds / 1000)
    'If we are near midnight (or specify a very long time!) then finish could be
    'greater than the maximum possible value of timer. Bring it down to sensible
    'levels and count the number of midnights
    While finish >= 86400
        finish = finish - 86400
        days = days + 1
    Wend

    Dim lastTime As Single
    lastTime = Timer

    'When we are on the correct day and the time is after the finish we can leave
    While days >= 0 And Timer < finish
        DoEvents
        'Timer should be always increasing except when it rolls over midnight
        'if it shrunk we've gone back in time or we're on a new day
        If Timer < lastTime Then
            days = days - 1
        End If
        lastTime = Timer
    Wend

End Sub
于 2015-08-19T09:30:21.987 に答える
0

Steve Mallory の回答を使用しましたが、タイマーが 86400 または 0 (ゼロ) シャープ (MS Access 2013) に到達しないか、少なくとも時々到達しないことを恐れています。だから私はコードを修正しました。真夜中の条件を「If Timer >= 86399 Then」に変更し、次のようにループ「Exit Do」のブレークを追加しました。

Public Function Pause(NumberOfSeconds As Variant)
    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim Start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Elapsed = 0
    Do While Timer < Start + PauseTime
        Elapsed = Elapsed + 1
        If Timer >= 86399
            ' Crossing midnight
            ' PauseTime = PauseTime - Elapsed
            ' Start = 0
            ' Elapsed = 0
            Exit Do
        End If
        DoEvents
    Loop

Exit_GoTo:
    On Error GoTo 0
    Exit Function
Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function
于 2015-05-01T17:48:25.260 に答える
-1

Due クレジットと Steve Mallroy に感謝します。

Wordで深夜の問題が発生し、以下のコードが機能しました

Public Function Pause(NumberOfSeconds As Variant)
 '   On Error GoTo Error_GoTo

    Dim PauseTime, Start
    Dim objWord As Word.Document

    'PauseTime = 10 ' Set duration in seconds
    PauseTime = NumberOfSeconds
    Start = Timer ' Set start time.

    If Start + PauseTime > 86399 Then 'playing safe hence 86399

    Start = 0

    Do While Timer > 1
        DoEvents ' Yield to other processes.
    Loop

    End If

    Do While Timer < Start + PauseTime
        DoEvents ' Yield to other processes.
    Loop

End Function
于 2015-11-18T14:09:09.400 に答える
-1

Windows ではタイマーは 100 分の 1 秒を返します... Macintosh プラットフォームではタイマーが整数を返すため、ほとんどの人は秒を使用します。

于 2014-03-11T21:53:18.767 に答える
-1

MS Access の場合: Me.TimerInterval を設定し、Form_Timer イベント ハンドラを使用して非表示のフォームを起動します。遅延させるコードを Form_Timer ルーチンに入れます - 各実行後にルーチンを終了します。

例えば:

Private Sub Form_Load()
    Me.TimerInterval = 30000 ' 30 sec
End Sub

Private Sub Form_Timer()

    Dim lngTimerInterval  As Long: lngTimerInterval = Me.TimerInterval

    Me.TimerInterval = 0

    '<Your Code goes here>

    Me.TimerInterval = lngTimerInterval
End Sub

「あなたのコードはここに入る」は、フォームが開かれてから 30 秒後に実行され、その後の各実行から 30 秒後に実行されます。

完了したら、非表示のフォームを閉じます。

于 2018-12-06T19:38:01.103 に答える