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

时间:2011-12-07 09:16:40

标签: excel vba excel-vba

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

如何使用ExcelVBA中向用户显示异步消息?

5 个答案:

答案 0 :(得分:8)

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

Application.StatusBar = "status message"

以下是有关如何实现此问题的示例: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

<强>更新: 我想补充说,更新状态栏将导致性能大幅下降(实际上相当多),因此您应该只在适当的时间间隔内更新它。这是我的意思的一个例子(我在这里使用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

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

答案 1 :(得分:4)

我坚持使用Walkenbach's progress form加入我的插件

enter image description here

答案 2 :(得分:2)

以下文章有多种方法:http://oreilly.com/pub/h/2607

我认为最好的选择是显示进度表。这可以包括进度条和文本更新,以使用户放心。

答案 3 :(得分:1)

我曾经做过的事情是创建一个名为“Running”的额外标签。 在每次耗时循环之后,我使用更新的文本信息添加以下代码。

虽然文本有时变化太快,但更改颜色条会向用户显示脚本仍在运行。您必须首先使用值6定义AlertColor。

Sheets("Running").Select 'Show the running page

Range("B18").Value = "Importing ABC......"

Cells(18, 2).Interior.ColorIndex = AlertColour

AlertColour = AlertColour + 1

If AlertColour > 8 Then AlertColour = 6

Application.ScreenUpdating = True

Application.ScreenUpdating = False

答案 4 :(得分:0)

我不知道您希望在多大程度上使用您的解决方案,但您可以使用RTD function。这样,您可以直接在工作表中放置状态消息。但是它需要开发一个COM Automation服务器,它并不复杂(可以用.NET或VB6或C ++或Delphi编写),但在生产中会产生问题(部署,支持,代码控制等)。