单元格闪烁VBA代码检查

时间:2016-02-14 07:59:50

标签: vba flashing

下面的代码会在值更改时闪烁一个单元格。它会保存原始cell b的值并保存并进行比较。 但是,当单元格的值基于另一个单元格或基于公式时,则此代码不起作用。

PrivateSub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("B1:B27")

IfNot Application.Intersect(KeyCells, Range(Target.Address)) _
IsNothingThen
If Target.Value > Cells(Target.Row,5).Value Then
'flash green
Target.Interior.ColorIndex =10
Pause 0.5
Target.Interior.ColorIndex =2
Pause 0.5
Target.Interior.ColorIndex =10
ElseIf Target.Value < Cells(Target.Row,5).Value Then
'flash red
Target.Interior.ColorIndex =3
Pause 0.5
Target.Interior.ColorIndex =2
Pause 0.5
Target.Interior.ColorIndex =3
EndIf
Cells(Target.Row,5).Value = Target.Value
EndIf
EndSub

'Pauses execution without holding up main UI thread
PublicFunction Pause(NumberOfSeconds AsVariant)
OnErrorGoTo Error_GoTo
Dim PauseTime AsVariant
Dim Start AsVariant

PauseTime = NumberOfSeconds
Start = Timer
DoWhile Timer < Start + PauseTime
DoEvents
Loop

Exit_GoTo:
OnErrorGoTo0
ExitFunction
Error_GoTo:
Debug.Print Err.Number, Err.Description, Erl
GoTo Exit_GoTo
EndFunction

0 个答案:

没有答案