+ Reply to Thread
Results 1 to 3 of 3

VBA - Sort data by custom list without selecting it or adding auto-filters

  1. #1
    Registered User
    Join Date
    05-26-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    18

    Talking VBA - Sort data by custom list without selecting it or adding auto-filters

    Hello,

    First post after using eveyone's help on here over last couple of years! My VBA skills are improving, but still gaps in my knowledge...

    I'm writing a fairly lengthy macro which involves sorting the data numerous times. The first couple of sorts are fairly straight forward and I'm currently doing it using the code below:



    Example Code

    With Sheets("Sheet1")

    With .Range("A7:AO" & LastRowCurrent)

    .Sort key1:=.Columns(4), order1:=xlAscending, _

    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

    DataOption1:=xlSortNormal

    End With

    End With



    I had previously used a lengthier bit of code which included putting an auto-filter on, but the version above runs through much quicker as I have 30,000+ lines of data to sort.

    However, I am unsure how to use a similar styled code to filter by a particular custom order sequence (i.e. not alphabetical or numerical)

    I would like the data to be sorted in the following order:

    "S1,S2,S3...S30,A1,A2,A3...A30,X1,X2,X3...X30"

    Can anyone help as I can't seem to work out how to code this using a similar style code to the example?

    Thanks.
    Last edited by wahbob; 05-26-2015 at 02:28 PM. Reason: Solved!

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA - Sort data by custom list without selecting it or adding auto-filters

    Use an array. Here is a good link

    http://www.cpearson.com/excel/SortingArrays.aspx

  3. #3
    Registered User
    Join Date
    05-26-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    18

    Re: VBA - Sort data by custom list without selecting it or adding auto-filters

    Thanks Stnkynts.

    I used a bit of the code from there to help get the solution.

    For anyone else who is looking for a similar solution, here's how my new code reads:


    Example Code

    Dim Arr(1 To 5) As String

    Arr(1) = "S1"
    Arr(2) = "S2"
    Arr(3) = "S3"
    Arr(4) = "A1"
    Arr(5) = "A2"

    'extend to cover full list - first 5 given as example.

    Application.AddCustomList ListArray:=Arr

    With Sheets("Sheet1")

    With .Range("A7:AO" & LastRowCurrent)

    .Sort key1:=.Columns(4), order1:=xlAscending, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal


    End With

    End With

+ 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: 1
    Last Post: 04-24-2014, 12:11 AM
  2. [SOLVED] VBA to Custom Sort Data Based on Referenced List
    By Kehoth01 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-21-2014, 05:51 PM
  3. selecting range for custom sort
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-27-2010, 03:15 AM
  4. Adding sort filters to multiple selected columns
    By johnlovesbeer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-22-2009, 05:12 AM
  5. [SOLVED] troubleshoot custom Excel Auto Filters when the column is a # or d
    By MEM in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-08-2005, 06:06 PM

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