Excel VBA“Please Wait”popup消息
我正试图在Excel-VBA中创build一个Please Wait
消息。 这是提醒用户处理sql查询的过程还在进行中。
我想这个消息显示在一个用户窗体中。
目前,用户连接到SQL DB,select数据库和其他一些选项,按下Go …,表单不会卸载。 在Go处,我有另外一个popup消息Please Wait
窗体,但这个窗体不能卸载,即sql执行命令不能运行。
我的代码调用Please Wait
表单,包括一个标签:
Call WaitShowSQl.ExecuteCall KillWaitCall WaitShow SQl.Execute Call KillWait
在另一个代码模块中我有:
Sub WaitShow() Wait.Show End Sub Sub KillWait() Unload Wait End Sub
我有以下代码调用waitform的userform:
Private Sub UserForm_Activate() Call PleaseWait End Sub Sub PleaseWait() Wait.Label1.Caption = "Calculating...Please Wait!" End Sub
现在,在执行代码时,运行时不会通过模块返回以执行以下操作:
SQl.ExecuteSQl.Execute
我可以用状态栏显示程序的进度,但用户窗口popup消息对于这个程序是非常有用的。
Form.Show方法的默认行为是将窗体显示为模式对话框。 你正在寻找的是一个无模式对话框,所以你需要指定,如下所示:
Sub WaitShow() Wait.Show vbModeLess End Sub
更新:
如果您无法使用无模式对话框,则可以使用其他选项,如注释中所述。 其中之一就是让等待的用户表单执行SQL。 这可以这样做:
在等待表单中,首先声明variables
Private mySomeParameter1 As String Private mySomeReturnValue1 As String 'Input parameters to the form Public Sub Init(ByVal some_parameter1 As String, ...) ... Initialize your SQL, NOT execute it End Sub 'Output values from the from Public Sub GetReturns(ByRef some_return_value1 As String, ...) ... set the output parameters... End Sub 'Do the work Private Sub UserForm_Activate() Call PleaseWait 'Either call DoEvents or a manual Refresh to let the label display itself Call ExecutSQL ' A function that executes the SQL Unload Me End Sub
主要forms是:
Sub WaitShow() Wait.Init(the parameters...) Wait.Show Wait.GetReturns(the results...) End Sub
还有一个select是完全跳过等待表单,而是在主表单上显示一个“等待”图像,并在SQL处理完成时隐藏它。
以下是在用户等待Excel完成处理时显示进度条的方法:
https://stackoverflow.com/a/10791349/138938
在你的情况下,你可以为你的查询猜测一个合理的“平均”时间,并用当时的百分比来更新进度条,这将使你的用户确信Excel正在工作,他们不需要惊慌。
我的解决scheme远不如其他的优雅和令人印象深刻。 仅供参考,我的脚本正在为FOR … NEXT循环中的许多合作伙伴进行处理,并且想要使用当前正在处理的合作伙伴更新屏幕。
我创build了一个名为“Messages”的工作表选项卡。 我尽可能地扩大了单元格A1的宽度和高度,并且select了一种我喜欢的颜色scheme的字体。 我的脚本开始closuresScreenUpdating。 那我
Sheets("Messages").Select Cells(1, 1).Value = "Processing " & Partner Application.ScreenUpdating = True Application.ScreenUpdating = False
之后,我回到我需要的任何表格。 没有什么奇特的,但它快速,简单,并做我所需要的。 🙂