在VBA中将ProgressBar UserForms显示为模态还是无模更好? [英] Is it better to show ProgressBar UserForms in VBA as modal or modeless?

查看:167
本文介绍了在VBA中将ProgressBar UserForms显示为模态还是无模更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VBA中将ProgressBar用户窗体显示为模态还是无模更好?在VBA中开发进度指标的最佳做法是什么?

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

无模式用户窗体需要使用Application.Interactive = False,而模态用户窗体就其本质而言会阻止与应用程序的任何交互,直到核心过程完成或被取消.

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.

但是,如果使用Application.Interactive = False,则Esc键会中断代码执行,因此在用户窗体和调用过程中都需要使用Application.EnableCancelKey = xlErrorHandler和错误处理(Err.Number = 18).

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.

资源密集型调用过程还会导致在无模式UserForms中错误触发CommandButton_ClickUserForm_Activate事件.

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.

但是,使用模式UserForms作为进度指示器的问题是,每个需要进度指示器的过程都需要一个单独的UserForm模块,因为调用过程必须在UserForm_Activate过程内部.

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.

因此,尽管在无模式UserForm中可以有一个可重复使用的进度指示器,但它比在多个模式UserForm中执行代码的可靠性要低.

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.

哪种方法更好?

谢谢!

推荐答案

我将结束这一讨论,并说Modal是赢家.我已经尝试过两种方法,但是最终您都试图用无模式的用户窗体来弥补太多的漏洞.模态更加严格,因为它更严格,但它鼓励您将代码分解为较小的块,从长远来看,这更好.

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.

这篇关于在VBA中将ProgressBar UserForms显示为模态还是无模更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆