DoEvents不执行事件...为什么? [英] DoEvents doesn't do the events... Why?

查看:63
本文介绍了DoEvents不执行事件...为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DoEvents在状态栏中(或工作表中的某些单元格中)强制更新进度指示器,如下面的示例代码所示.但是屏幕不刷新,或在某个时候停止刷新.任务最终完成,但是进度栏没有用.

I'm using DoEvents to force an update of a progress indicator in the status bar (or in some cell in the sheet) as in the example code below. But the screen doesn't refresh, or stops refreshing at some point. The task eventually completes but the progress bar is useless.

为什么不DoEvents执行事件"?我还能做些什么来强制屏幕更新?

Why won't DoEvents "do the events"? What else can I do to force a screen update?

我正在Windows XP上使用Excel 2003.

I'm using Excel 2003 on Windows XP.

这是对之前的问题的跟进;感谢 Robert Mearns 的回答和下面的示例代码.

This is a follow up to an earlier question; thanks to Robert Mearns for his answer and the sample code below.

Sub ProgressMeter()

Dim booStatusBarState As Boolean
Dim iMax As Integer
Dim i As Integer

iMax = 100

    Application.ScreenUpdating = False
''//Turn off screen updating

    booStatusBarState = Application.DisplayStatusBar
''//Get the statusbar display setting

    Application.DisplayStatusBar = True
''//Make sure that the statusbar is visible

    For i = 1 To iMax ''// imax is usually 30 or so
        fractionDone = CDbl(i) / CDbl(iMax)
        Application.StatusBar = Format(fractionDone, "0%") & " done..."
        ''// or, alternatively:
        ''// statusRange.value = Format(fractionDone, "0%") & " done..."

        ''// Some code.......

        DoEvents
        ''//Yield Control

    Next i

    Application.DisplayStatusBar = booStatusBarState
''//Reset Status bar display setting

    Application.StatusBar = False
''//Return control of the Status bar to Excel

    Application.ScreenUpdating = True
''//Turn on screen updating

End Sub

推荐答案

我发现DoEvents并不总是完全可靠的.我建议尝试两种不同的方法.

I've found DoEvents is not always completely reliable. I would suggest trying two different things.

首先,请尝试在状态栏更新之后(即,在您的Some code ....行之前)立即放置DoEvents调用.

First, try placing the DoEvents call immediately after the Status Bar update (ie, before your Some code .... line).

如果这不起作用,我发现在某些情况下,使用睡眠API是产生处理器时间的更可靠方法.如果DoEvents无法正常运行,通常这是我尝试的第一件事.您需要在模块顶部(函数之外)添加以下行:

If that does not work, I've found in some cases that using the Sleep API is a more reliable way to yield processor time. It's usually the first thing I try if DoEvents is not working as I'd like. You'll need to add the following line at the top of your module (outside of your function):

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

然后添加此行,以代替DoEvents:

Then add this line in place of, or in addition to, DoEvents:

    Sleep 1   'This will pause execution of your program for 1 ms

如果1毫秒不起作用,您可以尝试增加使用睡眠暂停程序的时间.

You might try increasing the length of time you pause the program using sleep if 1 ms doesn't work.

这篇关于DoEvents不执行事件...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆