Consulting

Results 1 to 5 of 5

Thread: Update Progress Bar at EXACTLY 1 second Interval

  1. #1

    Update Progress Bar at EXACTLY 1 second Interval

    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 :-

    [vba]
    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
    [/vba]

    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

  2. #2
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Not tested but how about this?

    [VBA]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 [/VBA]

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I think

    It's the DoEvents. If Events Done take longer than 1 second, the progress bar won't update until all events are done.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •