views:

474

answers:

2

Is it better to show ProgressBar UserForms in VBA as modal or modeless? What are the best practices for developing progress indicators in VBA?

Modeless UserForms require the use of Application.Interactive = False, whereas Modal UserForms by their very nature block any interaction with the application until the core procedure has finished, or is cancelled.

If Application.Interactive = False is used, however, the Esc key interrupts code execution, so the use of Application.EnableCancelKey = xlErrorHandler and error handling (Err.Number = 18) is required in both the UserForm and the calling procedure.

Resource intensive calling procedures can also result in CommandButton_Click and UserForm_Activate events misfiring in modeless UserForms.

In general, progress indicators that use modal UserForms seem simpler, because the code that is being executed is fully contained in the UserForm module, and there is less need for passing of variables.

The problem, however, with using modal UserForms for progress indicators is that a separate UserForm module is required for every procedure that needs a progress indicator, because the calling procedure has to be inside the UserForm_Activate procedure.

So, while it is possible to have a single reusable progress indicator in a modeless UserForm, it will be less reliable than executing the code from within multiple modal UserForms.

Which way is better?

Thanks!

+1  A: 

There's also a third way, using the Application.StatusBar. You can even simulate a true progress bar by using a sequence of U+25A0 and U+25A1 characters.

GSerg
Thanks for the suggestion GSerg, but I have tried using that in the past, but Excel 2007 stops updating the window and shows "Not Responding" at the top of the application window.
Kuyenda
I'm not too experienced with office 2007, but what if you call DoEvents right after setting a value to `StatusBar`?
GSerg
Huh, that worked. Wow, that's a nice option to have isn't it! Thanks GSerg!
Kuyenda
One problem with using the StatusBar for progress indicators is there is no way to provide a cancel button.
Kuyenda
A: 

I am going to close this one out and say Modal is the winner. I have tried both ways, but you end up trying to close too many loopholes with modeless userforms. Modal is more difficult because it is more strict, but it encourages you to break up your code into smaller chunks which is better in the long run anyway.

Kuyenda