Sort Excel worksheet with multiple colors

lostyankee

New Member
Joined
Jul 14, 2019
Messages
7
I have a spreadsheet that I need to sort that has multiple colors, sorting:
Blue
Red
Orange
Yellow

After this I have code to remove duplicates in the data, but I need to retain these in priority order, hence the need for sorting. Any ideas?

VBA Code:
Sub SortByColor()

    ActiveWorkbook.Worksheets("IYD All").Sort.SortFields.Clear
    ' Sort column B by Blue Priority
    ActiveWorkbook.Worksheets("IYD All").Sort.SortFields.Add(Range("b2"), _
        xlSortOnCellColor, xlAscending, , _
        xlSortNormal).SortOnValue.Color = RGB(0, 204, 255)
    ' Sort Column B by Red Priority
    ActiveWorkbook.Worksheets("IYD All").Sort.SortFields.Add(Range("b2"), _
        xlSortOnCellColor, xlAscending, , _
        xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
    ' Sort column B by Orange Priority
    ActiveWorkbook.Worksheets("IYD All").Sort.SortFields.Add(Range("b2"), _
        xlSortOnCellColor, xlAscending, , _
        xlSortNormal).SortOnValue.Color = RGB(255, 120, 0)
    ' Sort column B by Yellow Priority
    ActiveWorkbook.Worksheets("IYD All").Sort.SortFields.Add(Range("b2"), _
        xlSortOnCellColor, xlAscending, , _
        xlSortNormal).SortOnValue.Color = RGB(255, 255, 0)
  

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Please try the following on a copy of your workbook:
VBA Code:
Option Explicit
Sub Sort_Color()
    Dim ws As Worksheet, r As Range
    Set ws = Worksheets("IYD All")
    Set r = ws.Range("A1").CurrentRegion
    
    With ws.Sort
        With .SortFields
            .Clear
            .Add(ws.Range("B2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 204, 255)
            .Add(ws.Range("B2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
            .Add(ws.Range("B2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 120, 0)
            .Add(ws.Range("B2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 255, 0)
        End With
        .SetRange r
        .Header = xlYes
        .Orientation = xlTopToBottom
        .Apply
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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