+ Reply to Thread
Results 1 to 2 of 2

Custom Sort With 3 Keys

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Custom Sort With 3 Keys

    Hi all,
    I have a range which I am trying to custom sort with 3 keys.
    I am trying to construct two string arrays from two different columns on a sheet and use these to create two custom lists which I can sort with - the third key just sorting via a standard key.

    However, I cannot get the range to sort in the exact order of my customer lists and I believe it is just sorting ascending.

    Here is my code below:

    Sub SortIntoTeams()
    Dim LastRow As Long, LastColumn As Long, FormattedRange As Range
    LastRow = Sheets(1).Range("B65536").End(xlUp).Row
    LastColumn = Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column
    Set FormattedRange = Sheets(1).Range(Cells(8, 1), Cells(LastRow, LastColumn))
    
    Dim SortKey1 As Range, SortKey2 As Range, SortKey3 As Range
    Set SortKey1 = Sheets(1).Cells(7, 1)
    Set SortKey2 = Sheets(1).Cells(7, 10)
    Set SortKey3 = Sheets(1).Cells(7, 3)
    
    Dim sCustomList1() As String, sCustomList2() As String
    Dim x As Long, i As Long
    ReDim sCustomList1(1 To Sheets(1).Range("A65536").End(xlUp).Row)
    ReDim sCustomList2(1 To Sheets(1).Range("E65536").End(xlUp).Row)
    
    For x = 1 To Sheets(1).Range("A65536").End(xlUp).Row
        sCustomList1(x) = Sheets(2).Cells(x, 1)
    Next x
    For i = 1 To Sheets(1).Range("E65536").End(xlUp).Row
        sCustomList2(i) = Sheets(2).Cells(i, 5)
    Next i
    
    Application.AddCustomList ListArray:=sCustomList1
    Application.AddCustomList ListArray:=sCustomList2
    
    Sheets(1).Sort.SortFields.Clear
    FormattedRange.Sort Key1:=SortKey1, Order1:=xlAscending, Key2:=SortKey2, Order2:=xlAscending, Key3:=SortKey3, Order3:=xlAscending, Header:=xlGuess, _
        OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    Application.DeleteCustomList Application.CustomListCount
    
    End Sub

  2. #2
    Registered User
    Join Date
    03-03-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Custom Sort With 3 Keys

    Hi all,
    I might be over complicating my request, hence no replies.
    What I basically want to do is sort a range by two custom lists.
    Can anyone advise me on how this can be done?

+ 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. Disabling Custom Keyboard Keys
    By Drgenetix in forum Excel General
    Replies: 3
    Last Post: 06-05-2011, 05:40 AM
  2. Adding shortcut keys to Custom menus
    By ggayathri in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-17-2010, 05:38 AM
  3. custom navigation using cursor keys
    By baz0912 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-22-2008, 05:19 PM
  4. custom navigating using cursor keys
    By baz0912 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2008, 10:20 AM
  5. Custom sorts - Multiple Keys
    By Rafi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-14-2006, 08:30 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