如何在Excel中创建状态对话框

时间:2020-03-05 18:50:04  来源:igfitidea点击:

我已经在Excel中创建了数据库报告生成器。我正在尝试创建一个对话框,该对话框在程序运行时显示状态信息。

生成报告时,尽管会出现对话框,但无法刷新/更新其显示的信息。大多数情况下,对话框仅部分显示。我尝试使用.repaint方法,但仍然得到相同的结果。生成报告后,我只会看到完整的对话框。

解决方案

回答

该对话框也在同一UI线程上运行。因此,它太忙而无法自我重绘。不知道VBA是否具有良好的多线程功能。

回答

我已经使用Excel自己的状态栏(在窗口的左下角)来显示我过去开发的类似应用程序的进度信息。

如果我们只想显示进度的文本更新,则效果很好,并且根本不需要更新对话框。

好吧@JonnyGold,这是我用过的那种东西的一个例子...

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

希望这可以帮助!

回答

尝试在循环中添加DoEvents调用。那应该允许表单重画并接受其他请求。

回答

在Excel(XP或者更高版本)中执行操作时,以下代码可以很好地工作。

对于在Excel外部进行的操作(例如,连接到数据库并检索数据),此操作最大的好处是可以在操作之前和之后显示对话框(例如"获取数据","获取数据")

创建一个名为" frmStatus"的表单,在名为" Label1"的表单上放置一个标签。

将表单属性设置为" ShowModal" = false,这将允许代码在显示表单时运行。

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