如何在漫长的过程中通知用户?

BuZ*_*uZz 3 excel vba excel-vba

我有一些漫长的过程需要在连续的阶段向用户发出通知,这样他就不会相信Excel已经崩溃了.

如何Excel使用VBA?向用户显示异步消息?

aev*_*nko 8

您可以使用Excel中的状态栏执行此操作:

Application.StatusBar = "status message"
Run Code Online (Sandbox Code Playgroud)

以下是如何实现此示例:http://www.vbaexpress.com/kb/getarticle.php?kb_id = 87

下面是网站的代码(添加的换行符更容易阅读):

Sub StatusBar()

Dim x As Integer
Dim MyTimer As Double

'Change this loop as needed.
For x = 1 To 250
    'Dummy Loop here just to waste time.
    'Replace this loop with your actual code.
    MyTimer = Timer
    Do
        Loop While Timer - MyTimer < 0.03
        Application.StatusBar = _
        "Progress: " & x & " of 250: " & Format(x / 250, "Percent")
    DoEvents
Next x

Application.StatusBar = False

End Sub
Run Code Online (Sandbox Code Playgroud)

更新:我想补充一点,更新状态栏将导致性能大幅下降(实际上相当多),因此您应该只在适当的时间间隔内更新它.这是我的意思的一个例子(我在这里使用MOD来确保我们只增加1000):

Sub test()

Dim i As Long
Dim temp As String

For i = 1 To 1000000
    temp = "testing 123, testing 123"
    If i Mod 1000 = 0 Then
        Application.StatusBar = "Processing " & i & "/1,000,000..."
    End If
Next

Application.StatusBar = "Ready"

End Sub
Run Code Online (Sandbox Code Playgroud)

另请注意,您要将文本重置为"Ready",否则它将保留在循环中.

  • @TomJuergens:好的,所以让用户最初使用`MsgBox'了解它.按照下面状态栏中的进度."`. (2认同)