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

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

有帮助吗?

解决方案

下面的代码在 Excel(XP 或更高版本)中执行操作时效果很好。

对于在 Excel 之外发生的操作,例如连接到数据库并检索数据,它提供的最好的方法是在操作之前和之后显示对话框(例如, “获取数据”, “已获取数据”)

创建一个名为 “frm状态”, ,在表单上贴上一个标签,称为 “标签1”.

设置表单属性 '显示模式' = 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

其他提示

尝试添加一个 举办活动 在你的循环中调用。这应该允许表单重新绘制并接受其他请求。

我使用 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

希望这可以帮助!

在您的工作簿中插入空白表,重命名为EG。“信息”

Sheets("information").Select
Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")

继续宏

Sheets("information").Select
Range("C3").Select
Application.ScreenUpdating = True
ActiveCell.FormulaR1C1 = "Preparing Information"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")

继续宏

等等,或者选择一个空白单元格,而不是插入新表格

Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")

ETC

该对话框也在同一个 UI 线程上运行。所以,它太忙了,无法重新粉刷自己。不确定VBA是否具有良好的多线程功能。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top