Better way to delay macro other than Application.Wait or Application.Sleep?

為{幸葍}努か 提交于 2019-12-19 10:43:18

问题


I created a status bar that works off of application.wait. In a sense, that worked quite well. However, I want to be able to update the status bar while working on other worksheets. I am unable to find a suitable method. Below is what I have thus far.

Private i As Integer
Private newHour
Private newMinute
Private newSecond
Private waitTime

Private Sub UserForm_Activate()
For i = 1 To 10
    UserForm1.Label1.Width = UserForm1.Label1.Width + 24
    Application.Calculate
    If Application.CalculationState = xlDone Then
        newHour = Hour(Now())
        newMinute = Minute(Now())
        newSecond = Second(Now()) + 1
        waitTime = TimeSerial(newHour, newMinute, newSecond)
        Application.Wait waitTime
    End If
Next
UserForm1.Hide
End Sub

NOTE: For some odd reason the only way I could get the label to constantly update was to use Application.Calculate. Otherwise, it would wait the full 10 seconds, and then the status bar would reach maximum extension before hiding the userform.


回答1:


Use this delay function instead of Application.Wait

Private Sub delay(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub


来源:https://stackoverflow.com/questions/21385844/better-way-to-delay-macro-other-than-application-wait-or-application-sleep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!