+ Reply to Thread
Results 1 to 10 of 10

Reorder rows & copy to new sheet

  1. #1
    Registered User
    Join Date
    08-22-2009
    Location
    rome
    MS-Off Ver
    Excel 2003
    Posts
    4

    Reorder rows & copy to new sheet

    Hi,
    I need some VBA help in copying ranges from one sheet to another and pasting them on a predifined order. I have one worksheet that contains a large number of ranges with data on alphabetical order. Each entry on this sheet contains several rows of data and is identified by a name (e.g. entry "David" = rows 6:11, "Mark" = rows 13:18 and "Rose" = rows 20:25). I need to select certain ranges from this sheet and copy them on a predefined order on a different sheet (e.g 1) Rose, 2) David 3) Mark. The sequence is neither set by date, alphabetical order, nor by value. I have the order already predifined. I guess I could do this by specifiying the source sheet ranges and the destination ranges as well, but as I have over 2000 entries (each consisting of six rows), I need an efficient way to do it. Any help you could provide would be greatly appreciated.
    Last edited by mernst; 08-29-2009 at 07:04 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Reorder rows & copy to new sheet

    Welcome to the forum.

    Post a workbook.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-22-2009
    Location
    rome
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Reorder rows & copy to new sheet

    Many thanks,
    I am attaching a workbook with some extracts from the main (source) sheet. The predefined order would be the following:

    1. Rose
    2. David
    3. Mark

    4. Patricia
    5. Rani
    6. Paul

    7. Karl
    8. William
    9. Rene

    10. Olivia
    11. Mary
    12. Nancy
    13. Peter

    These ranges would need to be copied into a new sheet according to this predefined order. But as you can see, the order is completely subjective and I am at a loss as to how to define it within the code as the workbook in reality contains thousands of records. Again, any help you could provide would be greatly apreciated!
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Reorder rows & copy to new sheet

    First, duplicate the entries in cols A and B to eliminate blanks:

    1. Select A3:B92, and do Edit > Go to > Special > Blanks

    2. In the formula bar, type = then press the up arrow key, then press and hold the Ctrl key, then press Enter. Now all the blank cells copy the data above.

    3. Select columns A:B, copy, and paste-special values. Now the formulas are gone.

    If the above looks complex, it takes about 3 seconds ...

    Then in column M, number the rows to maintain the original order. In M3 enter 1, in M4 2, and then drag to the bottom of the list (90).

    Then make a list that defines the sort order -- essentially the list in your prior post, sans spaces and the numbers at left. Just the names in order. Put it on a separate sheet or anyplace out of the way. Select the list and name it, say, myOrder.

    In N3 and copy down, MATCH(B3, myOrder, 0)

    You'll notice that the name Dave appears in col B, but David appears in myOrder. Fix it one place or the other.

    Then select A:M and sort by column N and M.
    Last edited by shg; 08-23-2009 at 01:01 PM.

  5. #5
    Registered User
    Join Date
    08-22-2009
    Location
    rome
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Reorder rows & copy to new sheet

    Many thanks for your reply!!! In fact I had previously thought of going about the problem with a custom sort procedure as well. However, my main problem is that the source workbook from which the data has to be reordered contains thousands of records that are very frequently updated. Unfortunately, I am not the end user of the workbook and I would not be able to count on the end user to reorder the sheets manually. As such, I was wondering whether there was a way to do this with a VBA procedure. Do you think it would be doable?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reorder rows & copy to new sheet

    Try this. It will create the sheets as needed, or clear then and insert the new data if the "name" sheets already exist.
    This macro assumes all the info for each person is grouped exactly as you've shown. It does not have anything in it that will work for the same name appearing multiple times in different sections down the data as you did not demonstrate that as a need.

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reorder rows & copy to new sheet

    Quote Originally Posted by mernst View Post
    The predefined order would be the following:

    1. Rose
    2. David
    3. Mark

    4. Patricia
    5. Rani
    6. Paul

    7. Karl
    8. William
    9. Rene

    10. Olivia
    11. Mary
    12. Nancy
    13. Peter

    These ranges would need to be copied into a new sheet according to this predefined order.
    My apologies, I misread the need, you want them all on ONE sheet in that order?

    ...as you can see the order is completely subjective...the workbook in reality contains thousands of records.
    Now I'm lost. are the 13 names or are there 1000s?

    Or is it just the 13 names with 1000s of rows of data data grouped as shown but having from 5-100s of rows in each group?

    If it's 1000s of names, are you going to have an ordered list somewhere of the order in which want all of them to appear, like you've demonstrated for the first 13?
    Last edited by JBeaucaire; 08-23-2009 at 04:46 PM.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reorder rows & copy to new sheet

    If you create a sheet called NAMES and put the names in the order you want them to appear in column A, then run this macro.

    Note, you spelled "David" on your list of names, but in the data it was "Dave". You will have to correct your data or your name list for this to work.
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 08-23-2009 at 05:10 PM. Reason: Added ERROR trap for names in list not found in data

  9. #9
    Registered User
    Join Date
    08-22-2009
    Location
    rome
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Reorder rows & copy to new sheet

    Hi JBeaucaire,
    My apologies for the belated reply. Thank you so much!!!! The code worked perfectly!!! A rocket scientist indeed! Unfortunately, I did not explain myself properly. The workbook that uploaded was only an extract from the actual one and in reality there are thousands of entries in it. But I have just tested it and it works to perfection. You saved my life!

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Reorder rows & copy to new sheet

    Very glad to hear that! Thanks for letting us know.

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].

    (if the PREFIX box is no longer available, just add SOLVED to the start of the title)


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

+ 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.6.0 RC 1