+ Reply to Thread
Results 1 to 4 of 4

building a sort string

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2007
    Posts
    11

    building a sort string

    I have set up a userform for a context sensitive sort routine which selects the relevant sort keys (up to 3) and sets ascending/descending order. However. from this I need to be able to "build" the sort. I know the basic syntax eg

        Range("AI8:AM12").Select
        Selection.Sort Key1:=Range("AI9"), Order1:=xlAscending, Key2:=Range("AJ9" _
            ), Order2:=xlAscending, Key3:=Range("AL9"), Order3:=xlAscending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal

    I can vary the ranges to what I want (using Range (cells(x,y),cells(w,z))notation) but I need to be able to set the order for each sort key to either ascending or descending, and I need to be able to set up up to 3 sort keys. I can build up a suitable string to define the sort that I want but don't know how to "attach" the result to the sort statement. eg if I build
    sort$= 
    "key1:=Range (cells(5,12),cells(23,17)),Order1="&order$&"Header:=.
    ...etcetc"


    I can't simply type selection.sort sort$. How can I do it

    Any help greatly appreciated
    Last edited by dominicb; 09-24-2007 at 05:29 AM.

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning Andrew Hicks

    Try this little experiment - it should help demonstrate what you are trying to achieve. In the range A1:A10 put the numbers 1 to 10 in any order, then run this macro :

    Sub test()
    a = xlAscending
    Range("A1:A10").Sort Key1:=Range("A1"), Order1:=a
    End Sub
    Then change the value of "a" to xlDescending and run it again.

    By the way, please try and "wrap" your code next time you post - makes it much easier for others to read. Thanks.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    06-01-2007
    Posts
    11

    Smile Thank you

    Many thanks Dominic. Just what I wanted. It appears that xlAscending (or descending) is actually a numeric variable rather than a string variable.

    I have now set up my system to set the variable ad to either xlAscending or xlDescending according to the settings I want and I can allocate it as I build up the expression I want.

    Thanks also for the advice about wrapping code.

    I shall carry on experimenting but I am sure this has solved my problem.

    Again many thanks for your prompt and clear guidance

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi Andrew Hicks

    While xlDescending is a text string (to us humans), as with all xl (and vb) constants, it does have a numeric equivalent, which XL translates at run-time.

    To use the numeric eqivalents use 1 for ascending and 2 for descending.

    HTH

    DominicB

+ 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