How do I create a status dialog box in Excel
Question
I have created a database report generator in Excel. I am trying to create a dialog box that displays status information as the program runs.
When I generate the report, although the dialog box appears, I cannot refresh/update the information it displays. Most of the time, the dialog box only partially appears. I have tried using the .repaint method, but I still get the same results. I only see the complete dialog box, after the report is generated.
Solution
The code below works well when performing actions within Excel (XP or later).
For actions that take place outside Excel, for example connecting to a database and retrieving data the best this offers is the opportunity to show dialogs before and after the action (e.g. "Getting data", "Got data")
Create a form called "frmStatus", put a label on the form called "Label1".
Set the form property 'ShowModal' = false, this allows the code to run while the form is displayed.
Sub ShowForm_DoSomething()
Load frmStatus
frmStatus.Label1.Caption = "Starting"
frmStatus.Show
frmStatus.Repaint
'Load the form and set text
frmStatus.Label1.Caption = "Doing something"
frmStatus.Repaint
'code here to perform an action
frmStatus.Label1.Caption = "Doing something else"
frmStatus.Repaint
'code here to perform an action
frmStatus.Label1.Caption = "Finished"
frmStatus.Repaint
Application.Wait (Now + TimeValue("0:00:01"))
frmStatus.Hide
Unload frmStatus
'hide and unload the form
End Sub
OTHER TIPS
Try adding a DoEvents call in your loop. That should allow the form to repaint & accept other requests.
I have used Excel's own status bar (in bottom left of window) to display progress information for a similar application I developed in the past.
It works out very well if you just want to display textual updates on progress, and avoids the need for an updating dialog at all.
Ok @JonnyGold, here's an example of the kind of thing I used...
Sub StatusBarExample()
Application.ScreenUpdating = False
' turns off screen updating
Application.DisplayStatusBar = True
' makes sure that the statusbar is visible
Application.StatusBar = "Please wait while performing task 1..."
' add some code for task 1 that replaces the next sentence
Application.Wait Now + TimeValue("00:00:02")
Application.StatusBar = "Please wait while performing task 2..."
' add some code for task 2 that replaces the next sentence
Application.Wait Now + TimeValue("00:00:02")
Application.StatusBar = False
' gives control of the statusbar back to the programme
End Sub
Hope this helps!
Insert a blank sheet in your workbook Rename the Sheet eg. "information"
Sheets("information").Select
Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
Continue Macro
Sheets("information").Select
Range("C3").Select
Application.ScreenUpdating = True
ActiveCell.FormulaR1C1 = "Preparing Information"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
Continue Macro
Etc Alternatively select a blank cell somewhere on the existing sheet instead of inserting a new sheet
Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
Etc
The dialog box is also running on the same UI thread. So, it is too busy to repaint itself. Not sure if VBA has good multi-threading capabilities.