+ Reply to Thread
Results 1 to 16 of 16

Sort more than one range?

  1. #1
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Sort more than one range?

    Hi folks,
    I am very new to VBA and have been pecking through it clumsily.
    I've created this macro to sort a range:

    _________________________________________________________________
    Please Login or Register  to view this content.
    __________________________________________________________________

    It works perfectly. But now I need a new range to sort along with A4-I141.
    I simply don't know how to add this range into the mix.
    I've tried a union but I must have syntax wrong because it throws me errors.
    I am sure there's an easy way to include cell range AF4:AP141 so it will sort along with A4-I141.
    I would assume defining the new range is part of the process but I don't know the proper way to do this.
    Can someone help a rookie VBA guy with an easy way to add this new range into the mix?

    Thanks!
    Last edited by AliGW; 12-16-2019 at 11:17 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Sort more than one range?

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. As you are new here, I shall do it for you this time.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    Sorry I'm very new here and still working through proper procedures.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Sort more than one range?

    You can't sort two separate ranges at the same time (either manually or in code). Is there a particular reason the range in between shouldn't sort?
    Rory

  5. #5
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    The code I have above is working fine.
    I need an additional range to sort along with the range A4:I141.
    In other words, range AF4:AP141 need to line up with the original range when the sort happens.
    When sorting, say the value in A4 ends up in A50.
    I would need AF4 to end up in AF50 as well.
    I just don't know how to add another range into the mix.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Sort more than one range?

    Sorry I'm very new here and still working through proper procedures.
    Just have a read of the rules (it's all there): https://www.excelforum.com/forum-rul...rum-rules.html

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Sort more than one range?

    As I said, that won't work. I'd suggest you either rearrange your worksheet so that the part that needs to sort is all in one block, or post a copy of your workbook so that we can figure out the best alternative.

  8. #8
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    I've attached a sample sheet.
    In the sample I want to sort A2:B6 and have G2:H6 to sort along with it.
    The cells in between in my master sheet have data so I can't rearrange the sheet.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi,

    unclear so according to your attachment join as well the expected result workbook …

  10. #10
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    If I sort by Customer name alphabetically then "AJ" in position B6 would move up to cell B4.
    I need G6 and H6 to move to positions G4 and H4 respectively when that sort happens.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question


    And what happens to C:F cells ? As you can filter the global range A:H at once ! …

  12. #12
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    Quote Originally Posted by Marc L View Post

    And what happens to C:F cells ? As you can filter the global range A:H at once ! …
    This is a sample.
    In my master file there is data in those cells that cannot be changed or sorted with the other cells.
    The sort macro needs to ignore those cells.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    So just copy the 'key sort' column B to column I and apply the same sort on both ranges then delete column I …

  14. #14
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: Sort more than one range?

    Of course one can write own sorting procedure.
    For instance if we decide to go for bubble sort algorithm, a simple implementation could look like this:

    Please Login or Register  to view this content.
    Try it :-)
    Attached Files Attached Files
    Best Regards,

    Kaper

  15. #15
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Sort more than one range?

    Depending on the nature of the actual file, you might also simply store the contents of the intervening cells in a variable, sort the entire block including the intervening cells, then write the old values/formulas back to the cells you didn't want sorted.

  16. #16
    Registered User
    Join Date
    12-13-2019
    Location
    Pennsylvania
    MS-Off Ver
    Office365
    Posts
    10

    Re: Sort more than one range?

    Quote Originally Posted by rorya View Post
    Depending on the nature of the actual file, you might also simply store the contents of the intervening cells in a variable, sort the entire block including the intervening cells, then write the old values/formulas back to the cells you didn't want sorted.
    Sorry. I've been offline for a few weeks and didn't want to leave this topic hanging.
    That's essentially what I ended up doing. I thought about inserting columns and moving the additional data to the adjacent cells in the search range then just expanding my original range in the macro.
    But I used some empty cells as helpers and got the sorting to work like I want with VLOOKUP formulas.
    So when the macro sorts the data, the VLOOKUP finds it in the same range that the macro uses and moves the cell contents to that corresponding row.

    Thank you all for your help.
    I'll mark the thread as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 12
    Last Post: 09-08-2018, 10:34 PM
  2. Using custom sort orders with Range().sort
    By WestWindsDemon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2018, 03:52 PM
  3. Cannot Sort Range VBA - "Unable to get sort property of range class"
    By jkj115 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2015, 04:51 PM
  4. [SOLVED] Issue with Sorting a Range: Sort method of Range class failed
    By helpmeinexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2013, 01:51 PM
  5. sort button - how to sort large range in boxes
    By jw01 in forum Excel General
    Replies: 2
    Last Post: 03-06-2012, 09:35 PM
  6. Select 1st 2d range, sort, find 2nd, sort, etc
    By finny388 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-24-2011, 03:12 PM
  7. Replies: 1
    Last Post: 06-17-2006, 09:10 PM

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