Why does Excel inconsistently manage the cut rows range during a cut and paste to a new sheet?Cutting Row with Data and moving to different sheet VBAExcel VBA remove blank row after cutCut + Insert sometimes deletes and shifts cells, other times it doesn't. Why?In Excel VBA, how can I test if an Excel.Range object variable loses its reference without raising runtime error 424..?Cut Paste data from excel sheet using Autofilter in Excel VBAWeird Excel bug when deleting sheet with named ranges?Cut and Paste Columns in Excel Range with c#Search for text in a string, copy & paste rows to new sheetVBA Macro code fail; Copying and Pasting from range into new rangecopy data from only 3 sheets and paste it in new sheet - ExcelLoop through excel spreadsheets in different workbooksIf cell = closed project, cut cell and past into “Projects closed” worksheet on next available rowExcel VBA: Trigger macro on cut/paste/delete/insert eventsExcel VBA - Routine that finds on one sheet and paste on another moving destination cells down

Is it rude to call a professor by their last name with no prefix in a non-academic setting?

Count Even Digits In Number

Is the derivative with respect to a fermion field Grassmann-odd?

I unknowingly submitted plagarised work

Website returning plaintext password

Caught 2 students cheating together on the final exam that I proctored

Python program to find the most frequent letter in a text

Make 24 using exactly three 3s

Can a person survive on blood in place of water?

Alignment: "Breaking out" of environment (enumerate / minipage)

Python program to take in two strings and print the larger string

Can I tell a prospective employee that everyone in the team is leaving?

Could a 19.25mm revolver actually exist?

What are these arcade games in Ghostbusters 1984?

number headings

How should I introduce map drawing to my players?

Are these reasonable traits for someone with autism?

Why did the person in charge of a principality not just declare themself king?

Why did David Cameron offer a referendum on the European Union?

Count rotary dial pulses in a phone number (including letters)

Inconsistent results from Wolfram Could

Using credit/debit card details vs swiping a card in a payment (credit card) terminal

What are the real benefits of using Salesforce DX?

Where's this lookout in Nova Scotia?



Why does Excel inconsistently manage the cut rows range during a cut and paste to a new sheet?


Cutting Row with Data and moving to different sheet VBAExcel VBA remove blank row after cutCut + Insert sometimes deletes and shifts cells, other times it doesn't. Why?In Excel VBA, how can I test if an Excel.Range object variable loses its reference without raising runtime error 424..?Cut Paste data from excel sheet using Autofilter in Excel VBAWeird Excel bug when deleting sheet with named ranges?Cut and Paste Columns in Excel Range with c#Search for text in a string, copy & paste rows to new sheetVBA Macro code fail; Copying and Pasting from range into new rangecopy data from only 3 sheets and paste it in new sheet - ExcelLoop through excel spreadsheets in different workbooksIf cell = closed project, cut cell and past into “Projects closed” worksheet on next available rowExcel VBA: Trigger macro on cut/paste/delete/insert eventsExcel VBA - Routine that finds on one sheet and paste on another moving destination cells down






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have identified an inconsistency in the source range address for cut cells.



I have a utility in an Excel-based tool that migrates rows that the user selects. The user then must select the destination, which can be on the same sheet or on a different sheet.



The issue of Excel leaving blank lines when cutting and inserting into a different sheet are well known (c.f. Cutting Row with Data and moving to different sheet VBA and Excel VBA remove blank row after cut) and they have solutions for addressing it. However, my tool uses a different approach (similar to https://stackoverflow.com/a/27093382/9101981) - which I will now show does not work as intended.



Sub TestMigration()
'' Source Cells = migrateSource.Range("A3:A4") ' this simulates how the ranges are selected in the real world application
'' Target Cells = migrateTarget.Range("A7") ' this simulates the real world application where this is chosen through a pick box
MigrateRows migrateSource.Range("A3:A4"), migrateTarget.Range("A7")
End Sub

Sub MigrateRows(sourceCells As Range, targetCells As Range)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.EntireRow.Delete
End Sub


The source and target data is:



Source dataTarget data



The expected results are:



Expected source data after cut and insertExpected target data after cut and insert




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$3:$A$4




The actual results are:



enter image description hereenter image description here




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8




The data above shows that the source range has taken on address of the inserted cells, but not the worksheet name. If the cut cells address were to "move", I would expect it to also take on the new worksheet.



Additional information - what else did I try doing this?



  1. I tried to create a copy of the source cells. As this is copying a reference, I did not expect it to work, and was not disappointed.

Code:



Sub MigrateRows1(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Set tSourceRowsCopy = sourceCells
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.EntireRow.Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
'Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 1 (MigrateRows1)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried to create a new reference based on the address, but created this reference before the move.

Code:



Sub MigrateRows2(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Set tSourceRowsCopy = sourceCells.Worksheet.Range(tSourceAddress)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.Delete
' Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
' Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 2 (MigrateRows2)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried saving the address, and then creating a new reference just before the delete. This worked.

Code:



Sub MigrateRows3(sourceCells As Range, targetCells As Range)
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "Address to be deleted: " & sourceCells.Worksheet.Name & "," & tSourceAddress
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.Worksheet.Range(tSourceAddress).Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
End Sub









share|improve this question
























  • I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

    – Ahmed AU
    Mar 24 at 7:26











  • Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

    – AJD
    Mar 24 at 18:53

















0















I have identified an inconsistency in the source range address for cut cells.



I have a utility in an Excel-based tool that migrates rows that the user selects. The user then must select the destination, which can be on the same sheet or on a different sheet.



The issue of Excel leaving blank lines when cutting and inserting into a different sheet are well known (c.f. Cutting Row with Data and moving to different sheet VBA and Excel VBA remove blank row after cut) and they have solutions for addressing it. However, my tool uses a different approach (similar to https://stackoverflow.com/a/27093382/9101981) - which I will now show does not work as intended.



Sub TestMigration()
'' Source Cells = migrateSource.Range("A3:A4") ' this simulates how the ranges are selected in the real world application
'' Target Cells = migrateTarget.Range("A7") ' this simulates the real world application where this is chosen through a pick box
MigrateRows migrateSource.Range("A3:A4"), migrateTarget.Range("A7")
End Sub

Sub MigrateRows(sourceCells As Range, targetCells As Range)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.EntireRow.Delete
End Sub


The source and target data is:



Source dataTarget data



The expected results are:



Expected source data after cut and insertExpected target data after cut and insert




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$3:$A$4




The actual results are:



enter image description hereenter image description here




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8




The data above shows that the source range has taken on address of the inserted cells, but not the worksheet name. If the cut cells address were to "move", I would expect it to also take on the new worksheet.



Additional information - what else did I try doing this?



  1. I tried to create a copy of the source cells. As this is copying a reference, I did not expect it to work, and was not disappointed.

Code:



Sub MigrateRows1(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Set tSourceRowsCopy = sourceCells
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.EntireRow.Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
'Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 1 (MigrateRows1)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried to create a new reference based on the address, but created this reference before the move.

Code:



Sub MigrateRows2(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Set tSourceRowsCopy = sourceCells.Worksheet.Range(tSourceAddress)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.Delete
' Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
' Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 2 (MigrateRows2)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried saving the address, and then creating a new reference just before the delete. This worked.

Code:



Sub MigrateRows3(sourceCells As Range, targetCells As Range)
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "Address to be deleted: " & sourceCells.Worksheet.Name & "," & tSourceAddress
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.Worksheet.Range(tSourceAddress).Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
End Sub









share|improve this question
























  • I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

    – Ahmed AU
    Mar 24 at 7:26











  • Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

    – AJD
    Mar 24 at 18:53













0












0








0








I have identified an inconsistency in the source range address for cut cells.



I have a utility in an Excel-based tool that migrates rows that the user selects. The user then must select the destination, which can be on the same sheet or on a different sheet.



The issue of Excel leaving blank lines when cutting and inserting into a different sheet are well known (c.f. Cutting Row with Data and moving to different sheet VBA and Excel VBA remove blank row after cut) and they have solutions for addressing it. However, my tool uses a different approach (similar to https://stackoverflow.com/a/27093382/9101981) - which I will now show does not work as intended.



Sub TestMigration()
'' Source Cells = migrateSource.Range("A3:A4") ' this simulates how the ranges are selected in the real world application
'' Target Cells = migrateTarget.Range("A7") ' this simulates the real world application where this is chosen through a pick box
MigrateRows migrateSource.Range("A3:A4"), migrateTarget.Range("A7")
End Sub

Sub MigrateRows(sourceCells As Range, targetCells As Range)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.EntireRow.Delete
End Sub


The source and target data is:



Source dataTarget data



The expected results are:



Expected source data after cut and insertExpected target data after cut and insert




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$3:$A$4




The actual results are:



enter image description hereenter image description here




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8




The data above shows that the source range has taken on address of the inserted cells, but not the worksheet name. If the cut cells address were to "move", I would expect it to also take on the new worksheet.



Additional information - what else did I try doing this?



  1. I tried to create a copy of the source cells. As this is copying a reference, I did not expect it to work, and was not disappointed.

Code:



Sub MigrateRows1(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Set tSourceRowsCopy = sourceCells
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.EntireRow.Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
'Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 1 (MigrateRows1)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried to create a new reference based on the address, but created this reference before the move.

Code:



Sub MigrateRows2(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Set tSourceRowsCopy = sourceCells.Worksheet.Range(tSourceAddress)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.Delete
' Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
' Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 2 (MigrateRows2)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried saving the address, and then creating a new reference just before the delete. This worked.

Code:



Sub MigrateRows3(sourceCells As Range, targetCells As Range)
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "Address to be deleted: " & sourceCells.Worksheet.Name & "," & tSourceAddress
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.Worksheet.Range(tSourceAddress).Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
End Sub









share|improve this question
















I have identified an inconsistency in the source range address for cut cells.



I have a utility in an Excel-based tool that migrates rows that the user selects. The user then must select the destination, which can be on the same sheet or on a different sheet.



The issue of Excel leaving blank lines when cutting and inserting into a different sheet are well known (c.f. Cutting Row with Data and moving to different sheet VBA and Excel VBA remove blank row after cut) and they have solutions for addressing it. However, my tool uses a different approach (similar to https://stackoverflow.com/a/27093382/9101981) - which I will now show does not work as intended.



Sub TestMigration()
'' Source Cells = migrateSource.Range("A3:A4") ' this simulates how the ranges are selected in the real world application
'' Target Cells = migrateTarget.Range("A7") ' this simulates the real world application where this is chosen through a pick box
MigrateRows migrateSource.Range("A3:A4"), migrateTarget.Range("A7")
End Sub

Sub MigrateRows(sourceCells As Range, targetCells As Range)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.EntireRow.Delete
End Sub


The source and target data is:



Source dataTarget data



The expected results are:



Expected source data after cut and insertExpected target data after cut and insert




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$3:$A$4




The actual results are:



enter image description hereenter image description here




Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8




The data above shows that the source range has taken on address of the inserted cells, but not the worksheet name. If the cut cells address were to "move", I would expect it to also take on the new worksheet.



Additional information - what else did I try doing this?



  1. I tried to create a copy of the source cells. As this is copying a reference, I did not expect it to work, and was not disappointed.

Code:



Sub MigrateRows1(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Set tSourceRowsCopy = sourceCells
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.EntireRow.Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
'Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 1 (MigrateRows1)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried to create a new reference based on the address, but created this reference before the move.

Code:



Sub MigrateRows2(sourceCells As Range, targetCells As Range)
Dim tSourceRowsCopy As Range
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Set tSourceRowsCopy = sourceCells.Worksheet.Range(tSourceAddress)
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After cut source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "After insert source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then tSourceRowsCopy.Delete
' Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
' Debug.Print "After delete source_copy address: " & tSourceRowsCopy.Worksheet.Name & "," & tSourceRowsCopy.Address
End Sub



Test Run 2 (MigrateRows2)



Original source address: MigrateSource,$A$3:$A$4



After cut source address: MigrateSource,$A$3:$A$4



After cut source_copy address: MigrateSource,$A$3:$A$4



After insert source address: MigrateSource,$A$7:$A$8



After insert source_copy address: MigrateSource,$A$7:$A$8



Outcome: No change




  1. I tried saving the address, and then creating a new reference just before the delete. This worked.

Code:



Sub MigrateRows3(sourceCells As Range, targetCells As Range)
Dim tSourceAddress As String
tSourceAddress = sourceCells.Address
Debug.Print "Original source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
sourceCells.EntireRow.Cut ' the command used in the real world application
Debug.Print "After cut source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
targetCells.Insert
Debug.Print "After insert source address: " & sourceCells.Worksheet.Name & "," & sourceCells.Address
Debug.Print "Address to be deleted: " & sourceCells.Worksheet.Name & "," & tSourceAddress
If Not (sourceCells.Worksheet Is targetCells.Worksheet) Then sourceCells.Worksheet.Range(tSourceAddress).Delete
'Debug.Print "After delete source address: " & tSourceRows.Worksheet.Name & "," & tSourceRows.Address
End Sub






excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 4:33







AJD

















asked Mar 24 at 4:12









AJDAJD

1,5772514




1,5772514












  • I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

    – Ahmed AU
    Mar 24 at 7:26











  • Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

    – AJD
    Mar 24 at 18:53

















  • I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

    – Ahmed AU
    Mar 24 at 7:26











  • Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

    – AJD
    Mar 24 at 18:53
















I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

– Ahmed AU
Mar 24 at 7:26





I was also interested in loosing named range reference and searched for good reading in vain. May refer my comment in SO link1 also link2 terming it as expected excel behavior.

– Ahmed AU
Mar 24 at 7:26













Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

– AJD
Mar 24 at 18:53





Not quite the same. As you can see, I am not trying to reference the deleted range (which rightly gives an error) - this is about an inconsistency in a range that is moved.

– AJD
Mar 24 at 18:53












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320661%2fwhy-does-excel-inconsistently-manage-the-cut-rows-range-during-a-cut-and-paste-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320661%2fwhy-does-excel-inconsistently-manage-the-cut-rows-range-during-a-cut-and-paste-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript