代码之家  ›  专栏  ›  技术社区  ›  Johnno Nolan

在VBA中是否有与thread.sleep()等效的

  •  41
  • Johnno Nolan  · 技术社区  · 16 年前

    有等价物吗 Thread.Sleep() 在Access VBA中?

    8 回复  |  直到 8 年前
        1
  •  60
  •   Johnno Nolan    13 年前
    Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
    (ByVal dwMilliseconds As Long)
    

    使用以下语法调用sleep函数:

    Sub Sleep()
    Sleep 1000 'Implements a 1 second delay
    End Sub 
    
        2
  •  8
  •   DontFretBrett    13 年前

    另一种不使用kernel32的方法:

    Dim started As Single: started = Timer
    
    Do: DoEvents: Loop Until Timer - started >= 1
    
        3
  •  6
  •   Leo user370469    13 年前

    为了使代码正常工作,需要进行一些修改。 下面的代码是正确的版本。

    Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    
    Sub SleepVBA() 
    Sleep 1000 'Implements a 1 second delay 
    End Sub 
    
        4
  •  5
  •   The Data Brewer    11 年前

    所有其他使Excel等待的方法都会导致Excel完全没有响应。在确保用户界面响应的同时让Excel等待的解决方案是用等待的秒数调用这个等待子函数。

        Sub Wait(seconds As Integer)
          Dim now As Long
          now = Timer()
          Do
              DoEvents
          Loop While (Timer < now + seconds)
        End Sub
    
        5
  •  2
  •   Gaffi jusathr    13 年前

    我在Excel中使用它,效果很好:

    Application.Wait DateAdd("s", 1, Now())
    

    dateadd()是一个函数,它相对于 Now() (在本例中-您可以使用其他值作为参数), "s" 是时间度量(在本例中为秒),增量为1。在这里,函数调用告诉应用程序等待1秒。

    See also for more detail about the use of the DateAdd function.

        6
  •  2
  •   demongolem    11 年前

    可以使用Access VBA中的excel wait()过程。

    第一步是确保从项目中引用Excel库。

    完成后,以下代码将工作,等待10秒钟:

    Call Excel.Application.Wait(Time:=DateAdd("s",10,Now()))
    
        7
  •  2
  •   Tony L. ccalboni    8 年前

    如果你使用 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ,在对象模块中可能会出现此错误。

    enter image description here

    如果是这样,您可以将其声明为私有:

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

        8
  •  1
  •   sebastien leblanc    13 年前

    添加

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

    不知何故,在我的代码中的其他地方产生了额外的问题。 我最终使用了我在另一个论坛上找到的这个功能,并略作改动:

    Function WaitTime(n As Double)
    'Function that wait an amount of time n in seconds
    TWait = Time
    TWait = DateAdd("s", n, TWait)
    Do Until TNow >= TWait
         TNow = Time
    Loop
    End Function
    

    希望这有帮助:)