PDA

View Full Version : Update Progress Bar at EXACTLY 1 second Interval



agarwaldvk
04-08-2013, 04:51 AM
Hi Everybody


I have an application whereby I have a progress bar that is supposed to show the time elapsed for the current process. I have, with help from some of the code that I found on the internet, got some code that actually does that. However, I am not able to show the progress bar update EXACTLY at one (1) second interval.


The code is shown below :-


Sub Main()
Dim PctDone As Single
Application.ScreenUpdating = False
Dim a As Date, b As Date, aOrig As Date
aOrig = Now()
a = Now() + 1 / 24 / 60 / 60
b = Now() + 1 / 24 / 60 / 60 * 30
While Now() <= b
'Application.Wait (500)
If Now() >= a Then
PctDone = (a - aOrig) * 24 * 3600 '/ 30
With UserForm1
.Caption = Format(PctDone, "0")
.LabelProgress.Width = PctDone * 180 / 30
End With
a = Now() + 1 / 24 / 60 / 60
End If
DoEvents
Wend
Unload UserForm1
End Sub


The user form correctly displays the progress bar but it is not updated at EXACTLY one (1) second - sometimes it is done at one (1) and at other times, it is updated at two (2) second intervals.

Can I get it to update at EXACTLY one (1) second interval?

This is being done is Excel VBA.


Best regards



Deepak

sassora
04-08-2013, 02:06 PM
Not tested but how about this?

Sub Main()
Dim PctDone As Single
Dim aOrig As Date
Application.ScreenUpdating = False
aOrig = Now()
While Now() - aOrig <= time(0,0,30)
PctDone = (Now() - aOrig) / time(0,0,30)
With UserForm1
.Caption = Format(PctDone, "0")
.LabelProgress.Width = PctDone * 180 / 30
End With
DoEvents
Wend
Unload UserForm1
Application.ScreenUpdating = True
End Sub

SamT
04-08-2013, 04:43 PM
I think :104:

It's the DoEvents. If Events Done take longer than 1 second, the progress bar won't update until all events are done.

mdmackillop
04-09-2013, 05:59 AM
Try putting the updating in a separate timer macro; called by the Main and looking at PctDone as a public variable. Of course if PctDone does not change within the loop, you won't see any change in progress.

Paul_Hossler
04-09-2013, 06:43 PM
However, I am not able to show the progress bar update EXACTLY at one (1) second interval.


Out of courousity, why do you need to be so precise on a progress bar?

Most of the time, I'm just happy to see the blue line moving

Paul