My understanding ...
'Range' is an object
'Dim' allocates but does not create an instance of the object
'Set' creates an instance of the object
Each object has a pointer (handle) with it's allocated address in memory which ObjPtr will return as a Long
Test1 -- R1's address is 0 until it's instanticated and then it has a address
Test2 -- R2 is Set to R1 so it's address is the same and R1 'Is' R2 (same ObjPtr)
Test3 -- R1 and R2 are created separately but assigned the same group of cells. Even though they both contain the same block of cells, they are NOT the same object (different ObjPtr)
Test4 -- R1 is assigned a block of cells, and then with .Cut it is assigned a different block of cells, but it is the same object (ObjPtr did not change) and still exists
Test5 -- Same as Test4 with the commands spread out
Test6 -- The only way to 'release' an Object (on purpose at least) is to Set it to Nothing
Option Explicit
Sub test1()
Dim r1 As Range, r2 As Range
MsgBox ObjPtr(r1)
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
End Sub
Sub test2()
Dim r1 As Range, r2 As Range
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
Set r2 = r1
MsgBox ObjPtr(r2)
MsgBox r1 Is r2
End Sub
Sub test3()
Dim r1 As Range, r2 As Range
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
Set r2 = ActiveShworksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r2)
MsgBox r1 Is r2
End Sub
Sub test4()
Dim r1 As Range, r2 As Range
Worksheets("Sheet1").Range("A1").Value = 100
Worksheets("Sheet1").Range("A2").Value = 200
Worksheets("Sheet1").Range("B1").Value = 300
Worksheets("Sheet1").Range("B2").Value = 400
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
MsgBox r1.Address(1, 1, xlA1, True)
r1.Cut Worksheets("Sheet2").Range("X4:Y5")
MsgBox ObjPtr(r1)
MsgBox r1.Address(1, 1, xlA1, True)
End Sub
Sub test5()
Dim r1 As Range, r2 As Range
Worksheets("Sheet1").Range("A1").Value = 100
Worksheets("Sheet1").Range("A2").Value = 200
Worksheets("Sheet1").Range("B1").Value = 300
Worksheets("Sheet1").Range("B2").Value = 400
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
MsgBox r1.Address(1, 1, xlA1, True)
r1.Cut
Worksheets("Sheet2").Select
Worksheets("Sheet2").Range("X4:Y5").Select
Worksheets("Sheet2").Paste
MsgBox ObjPtr(r1)
MsgBox r1.Address(1, 1, xlA1, True)
End Sub
Sub test6()
Dim r1 As Range, r2 As Range
MsgBox ObjPtr(r1)
Set r1 = Worksheets("Sheet1").Range("A1:B2")
MsgBox ObjPtr(r1)
Set r1 = Nothing
MsgBox ObjPtr(r1)
MsgBox r1 Is Nothing
End Sub
My understanding at least.
Paul