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

我有一些长时间的stream程需要在后续阶段向用户发送通知,这样他就不会相信Excel已经崩溃了。

如何使用VBAExcel向用户显示asynchronous消息?

您可以在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 

另外请注意,您要将文本重置为“准备就绪”,否则会留在循环中。

我坚持Walkenbach的进步forms为我的插件

在这里输入图像说明

下面的文章有很多这样做的方法: http : //oreilly.com/pub/h/2607

我认为你最好的select就是展示一个进度表。 这可以包括一个进度条和文本更新来保证用户的安全。

我曾经做过的事情是创build一个名为“Running”的额外选项卡。 在每次耗时循环之后,我将下面的代码添加到更新的文本信息中。

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

 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 

我不知道你的解决scheme要走多远,但是你可以利用RTDfunction 。 这样你可以直接在工作表中放置一个状态信息。 但是这需要开发COM Automation服务器,这个服务器并不复杂(可以使用.NET或VB6或C ++或Delphi编写),但是在生产(部署,支持,代码控制等)时会导致问题。