Excel VBA:循环期间屏幕不刷新 [英] Excel VBA: Screen doesn't refresh during loop

查看:127
本文介绍了Excel VBA:循环期间屏幕不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使图像消失并在出现循环时重新出现.当我逐步执行代码时,该代码将按预期工作,但是当我运行它时,直到循环结束,屏幕才会更新.

I'm trying to make an image disappear and reappear while a loop is taking place. The code works as intended when I step through it, but when I run it the screen doesn't update until the loop is finished.

我尝试添加类似DoEvents和ActiveWindow.SmallScroll之类的内容,此处,但似乎没有任何效果.我觉得这个问题可能与我的PC/设置/Excel版本有关,并且该循环可能在某些人的机器上起作用.如果您想尝试,我已经在此处上传了示例文件.

I've tried adding things like DoEvents and ActiveWindow.SmallScroll as found here but nothing seems to work. I have a feeling this problem may have something to do with my PC/settings/version of Excel and that the loop may work on some peoples' machines. I've uploaded a sample file here if you want to try it.

我的代码是:

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

Sub ToggleImage()

For i = 1 To 20

Application.ScreenUpdating = True

ActiveSheet.Shapes("Picture 1").Visible = False
ActiveSheet.Shapes("Picture 2").Visible = True

ActiveSheet.Shapes("Picture 1").Visible = True
ActiveSheet.Shapes("Picture 2").Visible = False

Sleep 50


Next


End Sub

示例工作簿已随附.

推荐答案

DoEvents 必须有时间进行事件;-).因此,如果您睡一觉才叫它完全没用.它必须暂停期间起作用.

The DoEvents must have time to do events ;-). So it is totally useless if you call it once after a sleep. It must work during the pause.

以下应能工作:

Sub ToggleImage()

 Dim dTime As Double

 For i = 1 To 20

  'ActiveSheet.Range("a1").Value = i

  ActiveSheet.Shapes("Picture 1").Visible = False
  ActiveSheet.Shapes("Picture 2").Visible = True

  dTime = Time
  Do While Time < dTime + 1 / 24 / 60 / 60 / 2
   DoEvents
  Loop

  ActiveSheet.Shapes("Picture 1").Visible = True
  ActiveSheet.Shapes("Picture 2").Visible = False

  dTime = Time
  Do While Time < dTime + 1 / 24 / 60 / 60 / 2
   DoEvents
  Loop

 Next


End Sub

但是您将无法将暂停时间缩短到50毫秒.即使刷新工作表也需要更多时间.

But you will not be able shortening the pause to 50 milli seconds. Even the refreshing the sheet will take more time.

这篇关于Excel VBA:循环期间屏幕不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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