+ Reply to Thread
Results 1 to 10 of 10

Thread: move rows without copy/paste clipboard

  1. #1
    Registered User
    Join Date
    02-16-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Arrow move rows without copy/paste clipboard

    I know this is kind of a common topic but I'm having trouble finding the best solution.

    I'm simply trying to move a group of five rows (say Range("11:15") ) to another location ( Range("6:10") ) without over writing the location ( shift:=xlDown ). I can do this just fine with the code below. But I would like to do this without using the copy/paste clipboard. I've tried several ways, but they result in errors or unwanted results. Any suggestions?

    This does the job (but uses clipboard):
    RngSel.Cut
    RngSel.Offset(AtvEnd * 5).Insert shift:=xlDown

    Thanks for any help!
    Last edited by treyr; 06-22-2010 at 11:19 AM.

  2. #2
    Registered User
    Join Date
    01-25-2008
    Location
    The Netherlands
    MS-Off Ver
    2002, 2003, 2007
    Posts
    97

    Re: move rows without copy/paste clipboard

    Hi there,

    why do you want to do this without using the clipboard?
    J.

  3. #3
    Registered User
    Join Date
    02-16-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: move rows without copy/paste clipboard

    Hello Jevni,

    I was looking to avoid interfering with the user's clipboard. If something were stored on it, and this code ran, it would clear the user's info just so I could just move some cells. I figured there always seems to be a better way to directly reference the excel object model rather than resorting to the clipboard or selections.

    For example, I do not use .Select, as generated by the "Recorder", to write info to cells:
        Range("C4").Select
        ActiveCell.FormulaR1C1 = "Hello World"
        Range("C5").Select
    I would use:
        Range("C12").Value = "Hello World"

    Or not use copy/paste to copy info
        Range("C5").Select
        Selection.Copy
        Range("C8").Select
        ActiveSheet.Paste
    I would use:
        Range("C8").Value = Range("C5").Value

    I was hoping there was a way to move one range to the location of another range of the same size. Something like:
        MyRange1.cut Destination:=MyRange2 Shift:=xlDown
    This works without the "Shift:=xlDown", but certainly not with it.

    It's not a critical thing for my code to live without, I just thought there was a more "proper" way to move info around the object model than using the clipboard.

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: move rows without copy/paste clipboard

    If you cut or copy, it's going to the clipboard -- both clipboards. You can clear the Excel clipboard when you're done, following a Copy:
    Application.CutCopyMode = False
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    02-16-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: move rows without copy/paste clipboard

    That's defiantly something I should use to clean up with after I'm done, thanks shg and jevni.

    But I suppose there is no real way to move rows around without the use of the clipboard? I suppose even when I set the CutCopyMode to False, it still killed what was on the clipboard from before the code ran?

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: move rows without copy/paste clipboard

    No big deal:

    sub tst()
      sheets(1).rows(6).resize(5).insert
      sheets(1).rows(6).resize(5)=sheets(1).rows(16).resize(5).value
      sheets(1).rows(16).resize(5).delete
    End Sub

  7. #7
    Registered User
    Join Date
    02-16-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: move rows without copy/paste clipboard

    Wow,

    Very cool, I'll use that! Thanks for the help!

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: move rows without copy/paste clipboard

    Be aware that the formulas referring to the original cells will have #REF errors, and formulas will be replaced by values.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    02-16-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: move rows without copy/paste clipboard

    Ah, I'll make to sure to be cognizant of references to those cells. This problem would also exist if I use the copy/cut method correct?

    For the cell's formulas, the below code seems to work

    sheets(1).rows(6).resize(5)=sheets(1).rows(16).resize(5).Formula
    I think this is the more solid type of code I was looking for.

    Mucho thanks yall!

  10. #10
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: move rows without copy/paste clipboard

    This problem would also exist if I use the copy/cut method correct?
    If you copy, yes. If you cut, no.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0