[英]Excel VBA do while loop is killing performance
I wrote a macro to check if a date is the last day of a month.我写了一个宏来检查日期是否是一个月的最后一天。 If so this cell should blink every 1 second, so im calling a do while loop.
如果是这样,这个单元格应该每 1 秒闪烁一次,所以我调用了一个 do while 循环。
I want to start the Sub when I open the worksheet, so I added a Workbook_Open() Sub我想在打开工作表时启动 Sub,所以我添加了一个 Workbook_Open() Sub
Private Sub Workbook_Open()
Call CellBlink
End Sub
If the date is indeed the last day of the month this sub is getting called as expected.如果日期确实是该月的最后一天,则按预期调用此子。 The problem is, that the performance is so bad, that it is nearly impossible to work with this sheet.
问题是,性能太差了,几乎不可能使用这张纸。 It almost feels like this loop is getting called multiple times.
几乎感觉这个循环被多次调用。
Do While Today = EndOfMonth
CellThatBlinks.Interior.ColorIndex = 3
Application.Wait (Now + TimeValue("0:00:01"))
CellThatBlinks.Interior.ColorIndex = 0
Application.Wait (Now + TimeValue("0:00:01"))
CellThatBlinks.Interior.ColorIndex = 3
DoEvents
Loop
I would appreciate some help:)我会很感激一些帮助:)
Using Application.OnTime
is a way to loop without blocking execution.使用
Application.OnTime
是一种在不阻塞执行的情况下循环的方法。
First Name
the cell in the Workbook that you want to blink, eg "BlinkCell", using Formulas / Define Name.首先使用公式/定义
Name
命名工作簿中要闪烁的单元格,例如“BlinkCell”。
Then put this code in a Module (not a Workbook or Worksheet object):然后将此代码放入模块(不是 Workbook 或 Worksheet 对象)中:
Option Explicit
Dim strLast As String
Public Sub CellBlink()
Dim rngBlink As Range
If WorksheetFunction.EoMonth(Now, 0) = Int(Now) Then
Set rngBlink = Range("BlinkCell")
Dim onIndex, offIndex
onIndex = 3
offIndex = 0
If rngBlink.Interior.ColorIndex = onIndex Then
rngBlink.Interior.ColorIndex = offIndex
Else
rngBlink.Interior.ColorIndex = onIndex
End If
strLast = Format(Now + TimeValue("00:00:01"), "hh:mm:ss")
Application.OnTime strLast, "CellBlink"
End If
End Sub
Public Sub CancelBlink()
If Len(strLast) > 0 Then
Application.OnTime strLast, "CellBlink", Schedule:=False
Range("BlinkCell").Interior.ColorIndex = 0
End If
End Sub
and this code in the ThisWorkbook object: ThisWorkbook object 中的代码:
Option Explicit
Private Sub Workbook_Open()
CellBlink
End Sub
How it works: Once the Workbook_Open
event is fired, the global subroutine CellBlink
is called.工作原理:一旦触发
Workbook_Open
事件,就会调用全局子例程CellBlink
。 In the sheet, the blinking cell is Name'd "BlinkCell".在工作表中,闪烁的单元格命名为“BlinkCell”。 CellBlink checks whether today's date is the end of month: if it is then the cell colour is toggled (on->off->on etc).
CellBlink 检查今天的日期是否是月底:如果是,则切换单元格颜色(开->关->开等)。 Finally, the
Application.OnTime
function is called to run this same CellBlink macro in one second's time.最后,调用
Application.OnTime
function 以在一秒钟内运行相同的 CellBlink 宏。 The time that the macro is schedule to run is saved as a string.宏计划运行的时间保存为字符串。 Running the
CancelBlink
macro will terminate the loop until CellBlink
is called again.运行
CancelBlink
宏将终止循环,直到再次调用CellBlink
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.