Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why are my Range variables changed by "Cut"

Status
Not open for further replies.

TonyJollans

Programmer
Dec 18, 2002
7,186
GB
Hi All,

Whilst looking at a problem in another thread I came across something I can’t explain. Can somebody else, please?

Copying a range from one sheet to another in the same workbook doesn’t cause me any problem. Both displays in the code below give Sheet1 $A$7 Sheet1 $A$12 as I would expect.

Code:
 Dim tjStart As Range
Dim tjEnd As Range

Set tjStart = Worksheets("Sheet1").Range("A7")
Set tjEnd = Worksheets(“Sheet1”).Range("A12")

MsgBox tjStart.Worksheet.Name & “ “ & tjStart.Address & " " & tjEnd.Worksheet.Name & “ “ & tjEnd.Address

Range(tjStart, tjEnd).
Code:
Copy
Code:
 Destination:=Worksheets("Sheet2").Range("A1")

MsgBox tjStart.Worksheet.Name & “ “ & tjStart.Address & " " & tjEnd.Worksheet.Name & “ “ & tjEnd.Address

Cutting the same range causes my Range variables to be changed to reflect the cell range from the destination and the sheet from the source. This time the first display gives Sheet1 $A$7 Sheet1 $A$12 as before but the second gives Sheet1 $A$1 Sheet1 $A$6.

Code:
 Dim tjStart As Range
Dim tjEnd As Range

Set tjStart = Worksheets("Sheet1").Range("A7")
Set tjEnd = Worksheets(“Sheet1”).Range("A12")

MsgBox tjStart.Worksheet.Name & “ “ & tjStart.Address & " " & tjEnd.Worksheet.Name & “ “ & tjEnd.Address

Range(tjStart, tjEnd).
Code:
Cut
Code:
 Destination:=Worksheets("Sheet2").Range("A1")

MsgBox tjStart.Worksheet.Name & “ “ & tjStart.Address & " " & tjEnd.Worksheet.Name & “ “ & tjEnd.Address

If that’s just the way it works I can live with it (I’ll have to live with it) but I can’t find anything to suggest so. What am I doing wrong?

Thanks,
Tony

 
That's just the way it works. If you have a reference to a range object that is cut, the reference is redirected to the range that is relative A1 as the original range was relative to the range that was cut. For instance, if you have a reference to C5, and range B4:D6 is deleted, the reference to C5 will now be a reference to B2.

Personally, since a cut operation is simply a copy/delete, I would think the behavior would be the same as when you have a refence to a range that gets deleted, ie, the object reference is no longer valid.

A workaround to get the behavior I believe you desire, is to use the below snipped instead of the internal cut method.

Range(tjStart, tjEnd).Copy Destination:=Worksheets("Sheet2").Range("A1")
Range(tjStart, tjEnd).ClearContents

Jon Hawkins
 
Thanks Jon,

I did fear that was just the way it was but rather hoped not. Coding a workaround is not a problem; it's knowing when I need to.

Cut just removes data from cells; it does not remove the cells and I can't see any reason to change the reference. I somewhat naïevely thought that my private variables were under my own control - seems not. All part of the rich learning experience provided by Micro$oft.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top