Using a Progress Indicator in Macros

by Allen Wyatt
(last updated October 23, 2012)

1

The macro language (VBA) provided with Excel is quite powerful. This means that you can do some pretty intense (and impressive) tasks with the language. Some tasks might take quite a while to complete. The problem is that some users may think their computer has hung unless there is some on-screen indication that the macro is running.

There are two ways that you can approach a solution to such a task. The first (and simplest) option is to use the status bar to indicate the progress of your macro. For instance, you could add some code to your macro by which it determines the percentage of completion. Once you have a percentage in hand (let's say it is stored in the xPctComp variable), you simply add the following line to your macro:

Application.StatusBar = "Portion completed: " & _
  Format(xPctComp, "##0.00%")

Such a line could be added within the main body of your macro, for instance within whatever loop you have that controls processing. When the macro is just about done, you should add a line that clears the status bar, such as the following:

Application.StatusBar = ""

The other way to create a progress indicator is to use some sort of a dialog box that displays a "fuel gauge" indicator as to the percentage complete. There are numerous implementations of such a concept already available on the Web. A good representative of this approach is found at John Walkenbach's site:

http://www.j-walk.com/ss/excel/tips/tip34.htm

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2496) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Extracting a State and a ZIP Code

Excel is often used to process or edit data in some way. For example, you may have a bunch of addresses from which you need ...

Discover More

Leading Spaces in Document File Names

If you try to add spaces to the beginning of a document's file name, Word normally strips them away. This tip examines two ...

Discover More

Removing Blank Pages at the End of Your Document

You go to print out your document, and all of a sudden notice that there was a blank page that printed at the end. This could ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

MORE EXCELTIPS (MENU)

Checking if a Workbook is Already Open

Knowing of a workbook is already open can be a prerequisite to your macro working correctly. Here's how to check it out.

Discover More

Progression Indicator in a Macro

When your macro is humming along, minding its own business, a user watching the screen may not see any activity and therefore ...

Discover More

Understanding Functions in Macros

Functions are a common programming construct. They help you to create easy ways of processing information and returning a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 - 7?

2012-10-23 13:24:00

John Zelno

When implementing a progress indicator, be careful of how frequently the indicator is updated. Updating it too frequently can make a macro run much longer. Updating the screen with anything takes a lot of time. You should instrument the progress indicator with something to limit the update frequency. This can be a conditional (IF statement) that updates the conditional every 100 or 1000 loops, or every 5% of a task.

To prove this, I ran the following macro:

Sub test()
t1 = Timer
For i = 1 To 100000#
Application.StatusBar = "loop iteration: " & i
Next
t2 = Timer
For j = 1 To 100000#
If (j Mod 100) = 0 Then
Application.StatusBar = "loop iteration: " & j
End If
Next
t3 = Timer
For k = 1 To 1000000#
Next
t4 = Timer
MsgBox t2 - t1 & {carriage return} & t3 - t2 & {carriage return} & t4 - t3
End Sub

Running this, it takes 28.5 seconds for the macro to count to 100K, updating every loop, 0.3 seconds for it to count to 100K, updating every 100 loops, and 0.02 seconds to count to 1M without a status update.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing