代码之家  ›  专栏  ›  技术社区  ›  Keng

如何暂停特定时间?(Excel/VBA)

  •  93
  • Keng  · 技术社区  · 15 年前

    我有一个Excel工作表,其中包含以下宏。我想每秒钟循环一次,但如果我能找到执行此操作的函数,它就会挂起。不可能吗?

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Do
        Calculate
        'Here I want to wait for one second
    
    Loop
    End Sub
    
    13 回复  |  直到 6 年前
        1
  •  114
  •   kevinarpe Dario Hamidi    8 年前

    使用 Wait method :

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

    或(对于Excel 2010及更高版本):

    Application.Wait Now + #12:00:01 AM#
    
        2
  •  58
  •   LondonRob    7 年前

    将此添加到模块中

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

    或者,对于64位系统,使用:

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

    在宏中调用它,如下所示:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Do
        Calculate
        Sleep (1000) ' delay 1 second
    
    Loop
    End Sub
    
        3
  •  38
  •   Achaibou Karim    11 年前

    而不是使用:

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

    我更喜欢:

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

    因为以后读起来容易多了。

        4
  •  17
  •   clemo    11 年前

    这对我来说毫无瑕疵。在“do-until”循环之前或之后插入任何代码。在您的情况下,将5行(time1=&time2=&do-until“循环”)放在do循环的末尾。

    sub whatever()
    Dim time1, time2
    
    time1 = Now
    time2 = Now + TimeValue("0:00:01")
        Do Until time1 >= time2
            DoEvents
            time1 = Now()
        Loop
    
    End sub
    
        5
  •  12
  •   Gilles Gouaillardet    7 年前

    kernel32.dll中的sleep声明在64位Excel中不起作用。这将更为一般:

    #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
  •  5
  •   Brian Burns Yugansh    9 年前

    只是一个清理过的Clemo代码版本在Access中工作,它没有应用程序.wait函数。

    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
  •  3
  •   Nathaniel Ford iFail    10 年前

    Application.Wait Second(Now) + 1

        8
  •  2
  •   Stanley    10 年前
    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
  •  2
  •   Reverus    7 年前

    以下是睡眠的替代方法:

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

    在下面的代码中,我在一个旋转按钮上做了一个“发光”效果闪烁,如果用户“有问题”的话,我会将他们引向它,在循环中使用“sleep 1000”不会导致可见的闪烁,但是循环工作得很好。

    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
  •  1
  •   dave    10 年前

    我这样做是为了回答这个问题:

    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
    
        11
  •  1
  •   Anastasiya-Romanova 秀    6 年前

    我通常用 计时器 函数暂停应用程序。将此代码插入您的

    T0 = Timer
    Do
        Delay = Timer - T0
    Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
    
        12
  •  1
  •   cyberponk    6 年前

    大多数提供的解决方案都使用application.wait,它不考虑自currend second计数开始以来已经过的时间(毫秒),因此 它们的固有不精确性高达1秒。 .

    计时器方法是最佳解决方案 ,但是你必须考虑到午夜的重置,所以这里有一个非常精确的睡眠方法,使用计时器:

    'You can use integer (1 for 1 second) or single (1.5 for 1 and a half second)
    Public Sub Sleep(vSeconds As Variant)
        Dim t0 As Single, t1 As Single
        t0 = Timer
        Do
            t1 = Timer
            If t1 < t0 Then t1 = t1 + 86400 'Timer overflows at midnight
            DoEvents    'optional, to avoid excel freeze while sleeping
        Loop Until t1 - t0 >= vSeconds
    End Sub
    

    使用此项测试任何睡眠功能: (打开调试窗口:ctrl+g)

    Sub testSleep()
        t0 = Timer
        Debug.Print "Time before sleep:"; t0   'Timer format is in seconds since midnight
    
        Sleep (1.5)
    
        Debug.Print "Time after sleep:"; Timer
        Debug.Print "Slept for:"; Timer - t0; "seconds"
    
    End Sub
    
        13
  •  0
  •   DEV    11 年前

    试试这个:

    Threading.thread.sleep(1000)