excel VBA run macro automatically whenever a cell is changed

前端 未结 5 1629
礼貌的吻别
礼貌的吻别 2020-11-27 20:54

Is there a simple way to get Excel to automatically execute a macro whenever a cell is changed?

The cell in question would be in Worksheet(\"BigBoard\").Range(

5条回答
  •  死守一世寂寞
    2020-11-27 21:46

    Yes, this is possible by using worksheet events:

    In the Visual Basic Editor open the worksheet you're interested in (i.e. "BigBoard") by double clicking on the name of the worksheet in the tree at the top left. Place the following code in the module:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Me.Range("D2")) Is Nothing Then Exit Sub
            Application.EnableEvents = False 'to prevent endless loop
            On Error Goto Finalize 'to re-enable the events      
            MsgBox "You changed THE CELL!"
        End If
    Finalize:        
        Application.EnableEvents = True
    End Sub
    

提交回复
热议问题