我在 Excel 中创建了一个数据库报告生成器。我正在尝试创建一个对话框,在程序运行时显示状态信息。
当我生成报告时,虽然出现了对话框,但我无法刷新/更新它显示的信息。大多数情况下,对话框仅部分显示。我曾尝试使用 .repaint 方法,但仍然得到相同的结果。在生成报告后,我只看到完整的对话框。
我使用 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
在您的工作簿中插入一张空白工作表重命名工作表,例如。“信息”
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 是否具有良好的多线程功能。