VBA实时检测行号

时间:2015-05-20 02:32:33

标签: vba excel-vba excel

我有一段VBA代码可以在特定范围内替换行颜色。但它只在excel文件打开时运行一次。

当我需要更新备用颜色时,我正在使用按钮来运行VBA。有没有什么方法可以让VBA在实时中检测到行的变化?

提前感谢您的帮助

吉姆

1 个答案:

答案 0 :(得分:0)

是的,有一个名为Worksheet_Change的函数,只要一个单元格改变就会调用它。请尝试以下代码(将其粘贴到工作表的VBA编辑器中)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("A1:C10")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        ' Display a message when one of the designated cells has been 
        ' changed.
        ' Place your code here.
        MsgBox "Cell " & Target.Address & " has changed."

    End If
End Sub