我有一个查询数据库的宏(在这种情况下是一个excel工作簿),但是当宏查询数据库时,需要30秒,用户认为程序坏了。我试过“Application.StatusBar =”刷新文件。“”,但是用户看不到这句话,最好的方法是一个带有“稍等片刻”消息的msgbox,当宏完成时,此消息将关闭。你帮我写代码吗?我认为解决方案是这样的:
Sub Button1_Click()
'Call msgbox with message "Await a moment"
Dim x As Long
For x = 0 To 2000000000 Step 1
Next
'The msgbox is closed
End Sub
Run Code Online (Sandbox Code Playgroud)
我认为解决方案是 msgbox 但它是另一种选择,继续!
Msgbox 不起作用,因为它会停止执行所有内容,直到用户单击“确定”。
状态栏会起作用,如果您DoEvents在设置它后放置一个右键,以便系统有时间更新屏幕。
您唯一的其他选择是创建一个在处理时显示的特殊表单,但您还需要DoEvents在显示后显示。
Sub Test()
' create a userform and name it frmWait
' put whatever you want on that form
' that is what will be displayed while the user waits
frmWait.Show vbModeless
DoEvents
' do your processing here
Unload frmWait
End Sub
Run Code Online (Sandbox Code Playgroud)
或者:
Sub Test()
Application.StatusBar = "Refreshing File. "
DoEvents
' do your processing here
' reset the status bar to normal
Application.StatusBar = ""
End Sub
Run Code Online (Sandbox Code Playgroud)