While the functional use of VBA's Error-Trap is interesting, I have
many other tests in my Change() Sub. Errors in any of the other
sections of the procedure would have to be handled with the same
"do-nothing" routine; which may not be appropriate.
While it works, the "Not Intersect is Nothing" "double negative"
test seemes awkward. I selected Tou's because the address technique
seems more intuitive. Actually, looking at it, it appears to be "the
correct way". I tried, but just couldn't get the correct object
heirarchy/accessors.
However, I did test both techniques and show the actual tests here
for completeness should anyone else benefit.
Regards, Steve, Noskowicz.
P.S.
Because the code also does corrections on cell values, I turn off
events first in the Change() Sub [Application.EnableEvents = False]
and back on at the end [Application.EnableEvents = True].
' The original "fixed-cell" [direct address reference] code.
If Target.Address = "$X$3" Then
' Handle the change in cell X3
ElseIf Target.Address = "$X$4" Then
' Handle the change in cell X4
End If
The following techniques, using Cell Range-Names, allow relocating the
cells on the sheet without re-writing the code. Both techniques do
not error if a non-Named cell is changed.
Note that initially, cell X3 is RangeNamed Mem_First and X4 is
RangeNamed Mem_Last
"Address" technique:
If Target.Address = Sheet3.Range("Mem_First").Address Then
' Handle the change in cell X3
ElseIf Target.Address = Sheet3.Range("Mem_Last").Address Then
' Handle the change in cell X4
End If
"Intersect" technique:
If Not Intersect(Target, Range("Mem_First")) Is Nothing Then
' Handle the change in cell X3
ElseIf Not Intersect(Target, Range("Mem_Last")) Is Nothing Then
' Handle the change in cell X4
End If