This question is somewhat related to this one: VBA: What happens to Range objects if user deletes cells?
Is there any way to reliably test for identity of two cell references, and, more importantly, to store cell references over time?
Consider the following example:
Sub TestCellIdentity ()
Dim r As Range
Set r = Application.ActiveCell
r.Value = "Some Value"
Dim ws As Worksheet
Set ws = r.Worksheet
Dim n As String
n = ws.Name
Dim c As Range
Set c = Worksheets (n).Cells (r.Row, r.Column)
MsgBox ("ActiveCell ptr: " & CStr (ObjPtr (r)) & "Value: " & r.Value _
& "; Indirect access ptr: " & CStr (ObjPtr (c)) & " Value: " & c.Value)
End Sub
Running the example shows that ObjPtr (r) and ObjPtr (c) are different, even though they refer to the same cell. What is worse, it seems to me (from some tests) that I cannot assume that two cell objects (object references) with the same ObjPtr reference are guaranteed to refer to the same cell (i.e. I cannot store the ObjPtr value and use it, e.g., as a key somewhere).
Hence my question: How can I uniquely identify a cell in Excel in a way that survives renaming, cutting, and/or pasting cells in the Worksheet?
A pointer is created each time a variable is assigned hence you get different references unless you assign one variable to another in which case it is the same pointer and has the same reference. Unless you use a Static declaration the reference will usually change each time you run the sub procedure. If you declare the pointer as Static and assign it once it should maintain a constant reference each time the code is run even if a user moves the cell. I ran this code to test it and it seems to work.