Paste this into the worksheet (not module or workbook) where the range is.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C1:CK3")) Is Nothing Then
For Each myCell In Range("C1:CK3")
If myCell.Value = "Txt1" Then
MsgBox ("Txt1")
ElseIf myCell.Value = "Txt2" Then
MsgBox ("Txt2")
ElseIf myCell.Value = "Txt3" Then
MsgBox ("Txt3")
ElseIf myCell.Value = "Txt4" Then
MsgBox ("Txt4")
ElseIf myCell.Value = "Txt5" Then
MsgBox ("Txt5")
ElseIf myCell.Value = "Txt6" Then
MsgBox ("Txt6")
End If
Next myCell
End If
End Sub
By using the Worksheet_Change with an if statement seeing if what was changed even affects your range, you minimize comparison work to be done only when those cells change.
I would recommend creating a named range (Ctrl+F3), single selecting each cell in that range (comma separated, not colon), and using the following code.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("ChangingRange")) Is Nothing Then
For Each myCell In Range("ChangingRange")
If myCell.Value = "Txt1" Then
MsgBox (myCell.Address(False, False) & "= Txt1")
ElseIf myCell.Value = "Txt2" Then
MsgBox (myCell.Address(False, False) & "= Txt2")
ElseIf myCell.Value = "Txt3" Then
MsgBox (myCell.Address(False, False) & "= Txt3")
ElseIf myCell.Value = "Txt4" Then
MsgBox (myCell.Address(False, False) & "= Txt4")
ElseIf myCell.Value = "Txt5" Then
MsgBox (myCell.Address(False, False) & "= Txt5")
ElseIf myCell.Value = "Txt6" Then
MsgBox (myCell.Address(False, False) & "= Txt6")
End If
Next myCell
End If
End Sub
This will also let you know exactly what cell has changed.
You can have a large named range (i.e. "ChangingRange") refer to the entire collection of cells, while a different name (i.e. "MSFT" or "DOW") can refer to one or more of the subset of cells. For example, "ChangingRange" = C1,D1,E1,F1....... etc. until EK3
"Schwab Financial" = D1,D2,E1,E2
This way, you would be able to return the more specific name associated with that range instead of just the cell address (more on this coming....)
It would also be easier to adjust your code when changes are made (and if you just adjust the named range, no adjustment to your code is necessary... much more portable and readable)
Thanks to
http://www.excelforum.com/excel-programming/391198-change-event-triggered-by-a-named-range.html for showing me how to react to a range specific worksheet change
Thanks to
http://www.tek-tips.com/faqs.cfm?fid=5254 for showing me how to return the letter-number cell address of a cell.