1

I had the same Problem as in this post. The answer that was given, did what was asked, it executed code when a given cell is changed.

It also worked for me, except when I try to change the name of the Sub to anything else, the code stops being executed. No warning is given. Does anyone know why that is?

This is the code that was the solution to the previous question:

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

    MsgBox "You changed THE CELL!"

    Application.EnableEvents = True
End Sub

or as a shorter Version

Private Sub Worksheet_Change(ByVal Target As Range)
    IF Target.Address = "$D$2" Then
        MsgBox("Cell D2 Has Changed.")
    End If
End Sub

Both Subs react the same way when I change their name. Is there a trigger somewhere that calls the macro, where I would have to also change the name?

Community
  • 1
  • 1
Kazschuri
  • 580
  • 1
  • 4
  • 18
  • 2
    The code stub `Private Sub Worksheet_Change(ByVal Target As Range)` is the event code that fires when the `Worksheet` object is changed. If you change the name, then the code won't automatically fire when worksheet is changed. – basodre Jul 24 '15 at 13:09

2 Answers2

2
Private Sub Worksheet_Change(ByVal Target As Range)

Is an event. It is called up by the system and HAS to be named like that

Here is a bit of reading for you to learn more

http://www.cpearson.com/excel/Events.aspx

Community
  • 1
  • 1
99moorem
  • 1,955
  • 1
  • 15
  • 27
2

You cannot change the name of a method for worksheet events.

This name is how the VBA knows that this method should be fired when something is changed in worksheet.

mielk
  • 3,890
  • 12
  • 19