做 While 循环以每秒闪烁单元格会降低性能
Do While loop to blink cell every second is killing performance
我写了一个宏来检查日期是否是一个月的最后一天。
如果是这样,这个单元格应该每 1 秒闪烁一次,所以我正在调用一个 Do While 循环。
我想在打开作品的时候启动Subsheet,所以加了一个Sub Workbook_Open()
。如果日期是该月的最后一天,则按预期调用此子。
Private Sub Workbook_Open()
Call CellBlink
End Sub
性能太差,几乎不可能使用这个 sheet。
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
使用Application.OnTime
是一种不阻塞执行的循环方式。
首先 Name
工作簿中要闪烁的单元格,例如“BlinkCell”,使用公式/定义名称。
然后将此代码放入模块(不是工作簿或工作sheet 对象):
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
ThisWorkbook 对象中的这段代码:
Option Explicit
Private Sub Workbook_Open()
CellBlink
End Sub
Private Sub Workbook_BeforeClose(Cancel as Boolean)
CancelBlink
End Sub
工作原理:触发 Workbook_Open
事件后,将调用全局子例程 CellBlink
。在 sheet 中,闪烁的单元格名称为“BlinkCell”。 CellBlink 检查今天的日期是否是月末:如果是,则单元格颜色会切换(开->关->开等)。最后,Application.OnTime
函数在一秒内被调用到 运行 这个相同的 CellBlink 宏。宏计划到 运行 的时间保存为字符串。 运行 CancelBlink
宏将终止循环,直到再次调用 CellBlink
。
我写了一个宏来检查日期是否是一个月的最后一天。
如果是这样,这个单元格应该每 1 秒闪烁一次,所以我正在调用一个 Do While 循环。
我想在打开作品的时候启动Subsheet,所以加了一个Sub Workbook_Open()
。如果日期是该月的最后一天,则按预期调用此子。
Private Sub Workbook_Open()
Call CellBlink
End Sub
性能太差,几乎不可能使用这个 sheet。
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
使用Application.OnTime
是一种不阻塞执行的循环方式。
首先 Name
工作簿中要闪烁的单元格,例如“BlinkCell”,使用公式/定义名称。
然后将此代码放入模块(不是工作簿或工作sheet 对象):
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
ThisWorkbook 对象中的这段代码:
Option Explicit
Private Sub Workbook_Open()
CellBlink
End Sub
Private Sub Workbook_BeforeClose(Cancel as Boolean)
CancelBlink
End Sub
工作原理:触发 Workbook_Open
事件后,将调用全局子例程 CellBlink
。在 sheet 中,闪烁的单元格名称为“BlinkCell”。 CellBlink 检查今天的日期是否是月末:如果是,则单元格颜色会切换(开->关->开等)。最后,Application.OnTime
函数在一秒内被调用到 运行 这个相同的 CellBlink 宏。宏计划到 运行 的时间保存为字符串。 运行 CancelBlink
宏将终止循环,直到再次调用 CellBlink
。