Sort selected range by font color

overbet

Board Regular
Joined
Jul 9, 2010
Messages
63
Office Version
  1. 2010
I am trying to get this code I found online to work for my need and cant figure out how to combine it. The first bit Sort_Selection works great to sort by value, but I need to sort by font color of selected range. This code is really terrific in that its not specific to a sheet name and such and someone like me can just put it into any sheet and change the column number so Id like to keep it that way. I recorded a macro and tried swapping out the sort info but I keep getting errors every way I can come up with to combine them. Does anyone know how to combine these so I can get it to sort selection by font colors? Thanks


Code:
Public Sub SortSelection()

'use the keyword "Selection" for the currently selected range
With ActiveSheet.Sort
    .SortFields.Clear
    'the key you want to use is the column to sort on. I used column 1, which is "A", column "B" is 2, etc
    .SortFields.Add Key:=Selection.Columns(3), Order:=xlDescending
    .SetRange Selection
    .Apply
End With


End Sub



Code:
Sub Macro26()

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C19670:C19727"), _
        xlSortOnFontColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(51, 102 _
        , 255)
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C19670:C19727"), _
        xlSortOnFontColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 0, _
        0)
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A19670:AM19727")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try something like that (I'm sorting by red colour font - RGB(255, 0, 0)):

Code:
Public Sub SortSelection()
    'use the keyword "Selection" for the currently selected range
    With ActiveSheet.Sort
        .SortFields.Clear
        'the key you want to use is the column to sort on. I used column 1, which is "A", column "B" is 2, etc
        .SortFields.Add(Selection.Columns(3), xlSortOnFontColor, xlDescending, xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
        .SetRange Selection
        .Apply
    End With
End Sub
 
Upvote 0
Thank you very much for taking the time to help me and answer my question. This is exactly what I needed, but could not figure out. People like you make this forum awesome for inexperienced users. Cheers JustynaMK
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,167
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top