Reputation: 1
Need a help on Cell Event Macro, I am testing Cell Event Macros and due to some reason its not triggering. Request your help to look into it
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("J8").Value = "No" Then
MsgBox "Hi"
Else
Exit Sub
End If
End Sub
Not Sure, where is the issue here, my expectation is to show a message box When I typed "No". However its not working
Upvotes: 0
Views: 36
Reputation: 14373
Below is the basic code you need and it must be installed in the code module of the sheet on which you want the action because the change event won't be noticed anywhere else.
Private Sub Worksheet_Change(ByVal Target As Range)
Const TriggerCell As String = "J8"
If Target.Address(0, 0) = TriggerCell Then
If Target.Value = "No" Then
MsgBox "Hi"
End If
End If
End Sub
This code is very crude, just for demonstrating the principle which is as follows.
Target
. That's the cell that was changed. If you use copy/paste, many cells could be changed at the same time. Then Target
would be a bigger range.TriggerCell
. You might specify an entire column (but not in the above setup which can handle only one cell).Target
provided by the event is the TriggerCell
you have specified you can compare its value with your expectation and take action depending upon the result of that comparison.Note that not Else
or Exit Sub
are required before the end of the sub.
Upvotes: 2