修改代码以检查范围单元格,然后仅闪烁包含该值的单元格

时间:2019-05-22 08:50:09

标签: excel vba

我有以下代码仅检查一个单元格(A1),如果该值等于1,则该单元格将闪烁并发出蜂鸣声,我想修改代码,以便它可以检查诸如以下范围的单元格(A1:A10)以获得特定值,并且仅闪烁/发出具有该值的单元格。 代码应该包括一件事;如果单元格值为空,则在循环中查找值时无需继续循环(例如,如果A5等于空白,则无需继续循环直到A10)。

Option Explicit
Public RunWhen As Double

Sub StartBlink()
    If Range("A1").Interior.ColorIndex = 3 Then
        Range("A1").Interior.ColorIndex = 6
        Beep
    Else
        Range("A1").Interior.ColorIndex = 3
        Beep
        End If
    RunWhen = Now + TimeSerial(0, 0, 1)
    Application.OnTime RunWhen, "StartBlink", , True
End Sub

Sub StopBlink()
    Range("A1").Interior.ColorIndex = xlAutomatic
    Application.OnTime RunWhen, "StartBlink", , False
End Sub

Option Explicit
Public CellCheck As Boolean

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

     'Change the IF statement and cell range to what you need
    If Range("A1") = "1" And CellCheck = False Then
        Call StartBlink
        CellCheck = True
    ElseIf Range("A1") <> "1" And CellCheck = True Then
        Call StopBlink
        CellCheck = False
    End If

End Sub

0 个答案:

没有答案