The Perfect Progress Bar
Forecasting Adjustable Rate Mortgages using Monte Carlo Simulation

The Perfect Progress Bar

A progress bar's purpose is to assure users our program is running and about when it will finish. The perfect progress bar does that and includes an optional Cancel button so users can kill processes that take too long. And it wouldn't be perfect if it wasn't simple to implement in Excel (NOTE! Windows version only!). The perfect progress bar:

  • Is just one object in our VBA (a userform) making it easy to import into other projects
  • Can be called in just 1 line of VBA
  • Has an optional cancel button to interrupt process that are taking too long
  • Can display messages in the progress bar and in the userform's title.
  • Can, in many cases, predict accurately at what time our process will end.

To build the user form we need 5 controls:

No alt text provided for this image

  1. frmProgress - The user form
  2. lblBack - A white label behind the expanding bar
  3. lblBar - An expanding gray label
  4. txtBar - A textbox with transparent background where we can display progress messages over the expanding bar.
  5. cmdCancel - An optional command button for killing the process

At the bottom of this post are each control's settings, but let's look at the form's code first.

No alt text provided for this image

When we initialize the form we set the bCancelled property to its default of FALSE. We then set the form's position to our workbook's center.

The QueryClose routine handles someone clicking the form's red X. It acts like clicking the Cancel button.

The form's Display method is our form's only interface. It returns vbOK or vbCancel. vbCancel indicates the user clicked the cancel button or the red X.

The only required parameter is fPercent which is the percentage complete (where 0.10 = 10%). This determines the bar's length.

All other parameters are optional. They add titles and text, as-well-as the Cancel button if we want to give users that option.

Here is an example on how we call the form.

No alt text provided for this image

We could call this test routine from the VBE immediate window like so:

Test 1000

In this example we have a loop calling oProgress.Display() repeatedly to update the progress bar. If our user clicks the Cancel button, the routine exits the loop.

To learn how to predict a process's ETA, follow how tBegin, tElapsed, and tEstimate are used in the code.

If you are wondering why I created a variable called oProgress to hold the userform, it is so we can display multiple progress bars simultaneously. This is helpful when we have a master process calling several lower level processes with the master process showing a high level progress while the lower level processes show their individual progress. Each of these processes can have their own instance of frmProgress running totally independent of other instances.

Here are each control's settings. Most settings are defaults so it does not take us long to adjust them.

No alt text provided for this image





Jan Karel Pieterse

Excel and VBA Specialist @ jkp-ads.com, Microsoft MVP

3 年

Nice one Craig Hatmaker . Will you also show the routine that uses the progress bar and how it handles a Cancel click?

要查看或添加评论,请登录

Craig Hatmaker的更多文章

  • Emulating Solver with LAMBDA

    Emulating Solver with LAMBDA

    Introduction Excel’s Solver is a powerful optimization tool, but some companies impose restrictions on its use. A…

    4 条评论
  • Rollingλ(): 5g Function for Rolling Calculations

    Rollingλ(): 5g Function for Rolling Calculations

    Introduction Excel's LAMBDA functions have opened up incredible possibilities. 5g functions are LAMBDA functions…

    10 条评论
  • TRIMRANGE() and Trim Refs

    TRIMRANGE() and Trim Refs

    I cringed when I first saw TRIMRANGE(). "Oh great," I thought, "another reason for people to avoid learning tables.

    6 条评论
  • Intro to LAMBDA - Lesson 16 - Simple Corkscrew

    Intro to LAMBDA - Lesson 16 - Simple Corkscrew

    I could use your help. I am preparing a virtual work session for FMI on "Intro to LAMBDA.

    13 条评论
  • Programming with LAMBDA - Prime Numbers

    Programming with LAMBDA - Prime Numbers

    LAMBDA makes Excel "Turing Complete." Virtually all programming languages are Turing complete.

    2 条评论
  • Timing LAMBDAs with LAMBDA

    Timing LAMBDAs with LAMBDA

    I recently created a 5g function. It worked great but two friends suggested faster ways to accomplish the same thing.

    2 条评论
  • Create a 5g Function: RunTotRowsλ()

    Create a 5g Function: RunTotRowsλ()

    NOTE! This article was written with assistance from Google's Gemini AI. Introduction In the world of Excel, complex…

    7 条评论
  • Live 5g Instruction

    Live 5g Instruction

    The only live 5g training session starts July 30th. Register here: https://maven.

    4 条评论
  • From Formulas to LAMBDAs

    From Formulas to LAMBDAs

    I have just completed creating a small class on converting a group of formulas into a single LAMBDA function. It is…

    1 条评论
  • Stairway to ... LAMBDA?

    Stairway to ... LAMBDA?

    I use Excel for everything. I need some stairs from my yard to the forest floor below.

    19 条评论

社区洞察

其他会员也浏览了