+ Reply to Thread
Results 1 to 8 of 8

Sort range by alpha and then by font color with VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-14-2014
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2013
    Posts
    328

    Sort range by alpha and then by font color with VBA

    I have a named range "Trade" (first column of range is A) which I would like to sort first by alpha and then by
    font color so that the black font items (ColorIndex = 1) will be on top and the pink font (ColorIndex = 7) items will be below.

    Can anyone help me with this?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Sort range by alpha and then by font color with VBA

    Details are a bit vague, but try something like this...

    Sub Sort_Trade()
        Dim i As Long
        Application.ScreenUpdating = False
        With Range("Trade")
            .Columns(2).Insert
            For i = 1 To .Rows.Count
                .Range("B" & i).Value = .Range("A" & i).Font.ColorIndex
            Next i
            .Sort .Range("A1"), xlAscending, .Range("B1"), , xlAscending, Header:=xlYes
            .Columns(2).Delete
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by AlphaFrog; 03-30-2014 at 09:53 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    02-14-2014
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2013
    Posts
    328

    Re: Sort range by alpha and then by font color with VBA

    Quote Originally Posted by AlphaFrog View Post
    Details are a bit vague, but try something like this...

    Thanks Alpha for helping out!

    Sorry that I wasn't clear enough.
    Right now your code is sorting, but only alpha, despite adding that column B and checking for the font values.

    I would like to sort by alpha AND by font colour.
    Is there a way to do it without creating that column B?
    I realize that it is deleted in the end.

    Thanks again!

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

    Re: Sort range by alpha and then by font color with VBA

    Try this
    Option Explicit
    
    Sub test()
        Dim a, i As Long, ii As Long, iii As Long
        Dim myColor As Long, n As Long, w, SL As Object
        Set SL = CreateObject("System.Collections.SortedList")
        With Range("myrange")   '<---- change here to real name
            a = .Value
            For i = 2 To UBound(a, 1)
                If Not SL.contains(a(i, 1)) Then
                    Set SL(a(i, 1)) = CreateObject("System.Collections.SortedList")
                End If
                myColor = .Cells(i, 1).Font.ColorIndex
                If Not SL(a(i, 1)).contains(myColor) Then
                    ReDim w(1 To UBound(a, 2), 1 To 1)
                Else
                    w = SL(a(i, 1))(myColor)
                    ReDim preservew(1 To UBound(a, 2), 1 To UBound(w, 2) + 1)
                End If
                For ii = 1 To UBound(a, 2)
                    w(ii, UBound(w, 2)) = a(i, ii)
                Next
                SL(a(i, 1))(myColor) = w
            Next
            With .Offset(1)
                .ClearContents: .Font.ColorIndex = xlAutomatic: n = 1
                For i = 0 To SL.Count - 1
                    For ii = 0 To SL.GetByIndex(i).Count - 1
                        With .Rows(n).Resize(UBound(SL.GetByIndex(i).GetByIndex(ii), 2))
                            .Value = Application.Index(SL.GetByIndex(i).GetByIndex(ii), 0, 0)
                            .Font.ColorIndex = SL.GetByIndex(i).GetKey(ii)
                        End With
                        n = n + UBound(SL.GetByIndex(i).GetByIndex(ii), 2)
                    Next
                Next
            End With
        End With
    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-14-2014
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2013
    Posts
    328

    Re: Sort range by alpha and then by font color with VBA

    Quote Originally Posted by jindon View Post
    Try this
    Option Explicit
    
                For ii = 1 To UBound(a, 2)
                    w(ii, UBound(w, 2)) = a(i, ii)
                Next
                
    End Sub
    Thanks very much jindon for helping out!
    I really appreciate it.

    I ran the code in debug mode and it kept cycling in a loop at the point I have indicated above. The loop would not end.

  6. #6
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Sort range by alpha and then by font color with VBA

    Do you still use Excel 2002 or later version? 2007 or later version may sort by colour.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

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

    Re: Sort range by alpha and then by font color with VBA

    Can you upload a small sample workbook with before/after format?

  8. #8
    Forum Contributor
    Join Date
    02-14-2014
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2013
    Posts
    328

    Re: Sort range by alpha and then by font color with VBA

    Thanks for helping out Izandol and Jindon.
    I am still using 2002.

    Today I will consider making a big change in my workbook!
    I think I will use two sheets instead of one for my calculations.

    That way I can put my 'active trades' (black) on one page and my 'scenario trades' on a second page (pink).

    By doing this, I wouldn't need to have two criteria for the sorts (alpha + color).
    For now I will mark the thread solved.

+ 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. Sort by font Color
    By Laslo1 in forum Excel General
    Replies: 9
    Last Post: 03-08-2012, 07:37 PM
  2. Sort by font color after sorting by separate variable
    By RedWing in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2011, 09:10 AM
  3. Sort a column based on Font Color
    By tdyl1969 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-16-2010, 11:30 AM
  4. sort cells by font color
    By baz0912 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2008, 02:09 PM
  5. [SOLVED] Sort or sub-total by Fill color or font color
    By Excel_seek_help in forum Excel General
    Replies: 1
    Last Post: 04-27-2006, 04:10 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