+ Reply to Thread
Results 1 to 2 of 2

Thread: Reorder Rows (Manual Sort), Copy, Insert, Delete Row

  1. #1
    Registered User
    Join Date
    03-03-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Thumbs up Reorder Rows (Manual Sort), Copy, Insert, Delete Row

    I am reletively new to VBA and a lot of web trawling was not yielding the answer to this simple question so hopefully this will help someone else.

    I was faced with having to reorder rows within subsets in a worksheet which could not be defined in a normal sort and had to be done through loops and If Then statements. I turns out this was not as difficult as I first thought and the code below shows it in simple fashion.

    Sub copy_insert()
    
    ' use loops and If Then logic to decide the original row
    ' position and the row where it should appear in the
    ' worksheet use variables for source and destination  
    ' in this example src and dest.
    
    src = 5
    dest = 2
    
        ' copy original "source" row
        Rows(src).Select
        Selection.Copy
        
        ' insert at new position in worksheet "destination"
        Rows(dest).Select
        Selection.Insert Shift:=xlDown
        
        ' delete original "source" row which is now a duplicate
        Rows(src + 1).Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlUp
    
    End Sub
    Hope this turns out to be useful to some other newby.

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

    Re: Reorder Rows (Manual Sort), Copy, Insert, Delete Row

    Since you are relately new to VBA you may not be familiar to avoid select and activate in VBA.
    The code you posted can also be written as:

    Sub copy_insert()
      src = 5
      dest = 2
    
      rows(dest).insert 
      rows(scr).cut rows(dest+1)
    End Sub



+ 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