+ Reply to Thread
Results 1 to 24 of 24

Sort a 2 dimension array

  1. #1
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Sort a 2 dimension array

    Hi folks,

    I've created a public array

    Please Login or Register  to view this content.
    This encompasses 2 header rows.

    blank, blank, blank, date, blank, etc...
    Record# Name owner data1 data2 etc
    3 Smith trust xyz abc etc
    4 Jackson joint jkl efg etc
    .
    .
    .
    250

    I need to sort this into a new array based on alphabetizing column 2 (Name) such that I can populate a multi-column listbox and then when a Name is selected from the list box, I can execute some code on the record involved.

    Thanks

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort a 2 dimension array

    Hi Tradinup2
    you can sort the data on a sheet previously
    for example
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    That sorts the sheet (header rows, user form buttons and all) but doesn't seem to create an array, so I tried this
    Please Login or Register  to view this content.
    Same result - sorts the sheet.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort a 2 dimension array

    It works for me
    Please Login or Register  to view this content.
    , Order3:=xlAscending is superfluous

  5. #5
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Sheet1 contains the data that the array "MyArray()" is created from. I don't want to sort that sheet, I need to create a new array "MyNewArray()" that contains the data from "MyArray" sorted by alphabetizing the second column "Name" on sheet1.

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort a 2 dimension array

    Okay, then try to use the Shell sort
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Thanks nilem,
    I have to take my wife to the Dr. and will be gone for a couple of hours, but I'll try that as soon as I get back.

  8. #8
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Nilem, That seems to work and sort quickly. Is there a way to create or re-dimension the array to allow for the 2 header rows?

    Also how can I put the array onto a temporary sheet named "Temp"?

    Thanks for the help.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    This is based on "QuickSort" algorithm.
    It will sort 2D array between specified row indexes.

    VSortM Variable(array), StartRowIndex, LastRowIndex, RefColumn, Order(1 for Ascending, 0 for Descending)
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Well, I spoke too soon as that still seems to sort the sheet that provides the data for the array.

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

    Re: Sort a 2 dimension array

    It would be easier to copy the array to an unused worksheet, sort it there, and then read it into memory.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Sort a 2 dimension array

    or
    if you want to sort by the second column of an array

    Please Login or Register  to view this content.



  13. #13
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Jindon -
    Please Login or Register  to view this content.
    The array is filled completely at the first stop. However, after the sort routine (second stop) the only part of the array that's left is the top two rows, so it seems like we've got the startrow and lastrow index reversed and I'm not smart enough to fix it.

    any further help appreciated.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    I thought you have header on 1st and 2nd row....

    If you don't have header, it should be 1 (from the top).
    Please Login or Register  to view this content.
    Is this what you wanted?

  15. #15
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Yes I do have 2 header rows, but after the sort the array and sheet14 only have the 2 header rows, so I thought maybe the routine is reversing the upper and lower bounds.

    we seem to be getting the exact opposite of what we want.

    thanks

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    I don't understand what you mean by opposite...

    Do you want to sort Descending?
    If so, the last arg should be 0
    Please Login or Register  to view this content.
    Otherwise I need to see your file.

  17. #17
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    I'll have to send the file tomorrow, but the problem is:
    we want sort the array ascending, but after the sort instead of getting rid of the 2 header rows, the only thing we have left is the 2 header rows.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    Did you see far down?

    Sort the array moves blanks at the top when sort ascending.

    It that a problem you've got?

  19. #19
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    yes that's the problem and we need to sort on the second column. Is there a way to re-dimension the array to check for the last row in colA and sort colB?

    thanks for your patience.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    Then reference column should be 2
    Please Login or Register  to view this content.
    If you don't have blanks in 2nd column, it should sort properly.

  21. #21
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Sorry, I should have picked up on the column variable.

    The blanks appear in the unfilled rows at the bottom of the range because I set the range in anticipation of the finished product. The array will always be dynamic however. Can I use this:
    Please Login or Register  to view this content.
    before the sort call and get rid of the unused rows at the bottom?

    For the sorted array to be available in all user forms and subs, do I need to declare it as Public MyArray as variant in module 1?

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Sort a 2 dimension array

    I really need to see your sheet layouts, but just try
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    I'll try that in the morning and send the sheets.

    thanks again.

  24. #24
    Registered User
    Join Date
    01-29-2013
    Location
    texas
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Sort a 2 dimension array

    Please Login or Register  to view this content.
    This did the trick.

    Thanks for your help & patience.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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