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

StackOverflow https://stackoverflow.com/questions/21385844

  •  03-10-2022
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top