+ Reply to Thread
Results 1 to 4 of 4

.Sort.SortFields.Add CustomOrder:=[USE A VARIABLE???]

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    ericbartha.com
    MS-Off Ver
    2016 Professional
    Posts
    126

    .Sort.SortFields.Add CustomOrder:=[USE A VARIABLE???]

    Hello!

    Does anyone understand why I cannot feed this a variable? I have tired using both a string and an integer, but it only works if I hardcode the value, "4", for example. Why!?

    Here is the code...

    Please Login or Register  to view this content.
    CustomOrder:="3" ---- "3" is where I want the variable so it is dynamic...

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: .Sort.SortFields.Add CustomOrder:=[USE A VARIABLE???]

    The argument CustomOrder is a Variant type. The Microsoft documentation on this is really poor. What I've been able to glean is that it requires a Variant array. I believe that when you use "4" or "3" VBA automatically casts that to an array of one element.

    Just as an educated guess, if you have a variable try this:

    Please Login or Register  to view this content.
    You may also find this link informative.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: .Sort.SortFields.Add CustomOrder:=[USE A VARIABLE???]

    Hello ericbartha,

    I don't have Excel 2016 but I doubt there has been any change to this property since the 2013 release.

    The CustomSort list contains both built-in and added lists. List 3 is a built-in list of short month names: Jan, Feb, Mar, etc. There should be no problem sorting even if the data in column "A" are note dates. The index should be an integer type (Short or Long) or Variant Integer or Variant Long. This is the offset into the CustomSort array of values.

    VBA problems related to compilation are generally caused by Registry fragmentation. You should defrag the registry. This requires third party software since it is not provided as part of the Office suite or Windows operating system.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    07-07-2014
    Location
    ericbartha.com
    MS-Off Ver
    2016 Professional
    Posts
    126

    Re: .Sort.SortFields.Add CustomOrder:=[USE A VARIABLE???]

    Thanks guys!

    So feeding it the Variant Array as @6StringJazzer suggested did not work, but that suggestion in combonation with a bit of info from the link he provided did.

    What I am trying to sort by is an Integer. I convert the integer to a string using Format( , "@") and then use CVar(newString) for CustomOrder:=.

    I do not get why this convert works but feeding it a variant directly does not. At least it works.

    Thanks to both of you. Here is the final code:

    Please Login or Register  to view this content.
    For a little context, the reason I needed this to work is because I have a dynamic named range that is used to populate the RowSource on a listbox.

    I do this by using: =OFFSET(table_ticketUpdates!$A$1,1,1,COUNTIFS(table_ticketUpdates!$A:$A,table_ticketUpdates!$A$2),2) in the Name Manager.

    Now, when the user makes their selection, it sorts the table by this info, so only their selection is then returned to the listbox. There might be an easier way to do this, but I think it is pretty slick.

    Thanks again!

+ 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. CustomOrder?
    By JKoo1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-21-2013, 12:35 PM
  2. Using variables in SortFields' Key code problems
    By bob33 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 05:26 PM
  3. [SOLVED] Does SortFields have a max of 64 items?
    By GJL65 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2012, 03:09 PM
  4. Replies: 6
    Last Post: 04-14-2012, 04:07 AM
  5. how to sort rows according to a variable
    By ltg in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-09-2008, 11:23 PM
  6. Variable Sort using VBA
    By solnajeff in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-16-2007, 07:22 AM
  7. Sort : how can I use a variable in a VB sort function?
    By El Bee in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-16-2006, 05:40 PM
  8. [SOLVED] Can I use a variable as a sort key in VB?
    By Pablo Bellissimo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-03-2005, 10:06 AM

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