Excel VBA 屏幕更新 - 仅更新一个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7084408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA Screen Updating - Update Only One Cell
提问by Thariq Shihipar
I have a macro that runs for about a minute and loads/processes a lot of data. Naturally I've turned off screen updating so that it runs faster and doesn't jump around all the time. However, I'd like to be able to turn on screen updating/force update one cell.
我有一个运行大约一分钟并加载/处理大量数据的宏。当然,我已经关闭了屏幕更新,以便它运行得更快并且不会一直跳来跳去。但是,我希望能够打开屏幕更新/强制更新一个单元格。
I can't just do something like:
我不能只是做这样的事情:
Application.ScreenUpdating = True
<SET VALUE>
Application.ScreenUpdating = False
Because when you set screen updating to true, it updates all the changes that have been made since you set it to false. How do I update just that one cell?
因为当您将屏幕更新设置为 true 时,它会更新自您将其设置为 false 以来所做的所有更改。我如何只更新那个单元格?
Thanks!
谢谢!
采纳答案by barrowc
Naturally I've turned off screen updating so that it runs faster and doesn't jump around all the time.
当然,我已经关闭了屏幕更新,以便它运行得更快并且不会一直跳来跳去。
The "doesn't jump around all the time" part makes me wonder if you are using Select
and Activate
a lot in your code. These are very time-consuming operations and you should be able to achieve most things without them.
在“不跳周围所有的时间”部分使我不知道,如果你正在使用Select
和Activate
大量的在你的代码。这些都是非常耗时的操作,如果没有它们,您应该能够完成大多数事情。
Judicious use of the Range
and/or Cells
properties of the Worksheet
in question should be a lot faster than any combination of Select
and/or Activate
. If you can speed up your code enough then you may be able to leave ScreenUpdating
on and thus solve your original problem
明智地使用Range
和/或Cells
有Worksheet
问题的属性应该比Select
和/或的任何组合快得多Activate
。如果您可以充分加速您的代码,那么您就可以继续ScreenUpdating
进行,从而解决您原来的问题
回答by Lance Roberts
Tie the cell to a textbox or other control, and let it show the value that you want to keep an eye on.
将单元格绑定到文本框或其他控件,并让它显示您想要关注的值。
回答by Vinny Roe
If speed is an issue, can you do:
如果速度是一个问题,您可以这样做:
Application.Calculation = xlCalculationManual
<process>
Application.Calculation =xlCalculationAutomatic
? That can make a big difference.
? 这可以产生很大的不同。
回答by ktr
Kind of a hack, but you could print an error message when the particular cell you want to be notified about is updated or [for something less intrusive] just update the status bar:
有点像 hack,但是当您要通知的特定单元格更新时,您可以打印错误消息,或者 [为了不那么干扰] 只需更新状态栏:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub test_screen_updating()
Range("A1:A10").ClearContents
Application.ScreenUpdating = False
For i = 1 To 10
Range("A" & i).Value = i
If i = 2 Then
' MsgBox "A2 was updated: " + CStr(Range("A2").Value)
Application.StatusBar = "A2 updated to: " + CStr([A2])
End If
Sleep 500
Next i
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
Just make sure you reset the status bar if you update it.
如果更新状态栏,请确保重置状态栏。