Sort values based on colors, then on values using VBA code

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
Hello, I'm tryin to sort values based on colors, then on values

The macro was working very well, but on one sheet, so I have to create a new module for each sheet, and because I have around 400 sheets,

This is the simple working macro:
VBA Code:
Sub A_Sort()
'
' A_Sort Macro
'

'
    Range("B4:J43").Select
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(169, _
        208, 142)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(244, _
        176, 132)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(184, _
        137, 219)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(155, _
        194, 230)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add2 Key:=Range("G4:G43") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Jan_3").Sort
        .SetRange Range("B3:J43")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B4:B43").Select
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add2 Key:=Range("B4:B43") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Jan_3").Sort
        .SetRange Range("B4:B43")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("C4").Select
End Sub

The best way is to run the macro for the active sheet, not for a named sheet

This my try, but it's not working, I don't know how to re-write the code to work on the active sheet

VBA Code:
Sub A_Sort()
'
' A_Sort Macro
'

'
    Dim WS As Worksheet

    Set WS = ActiveSheet

    With WS.Sort
    Range("B4:J43").Select
    WS.Range("G3:G43").Sort.SortFields.Clear
    WS.Range("G3:G43").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(169, _
        208, 142)
    WS.Range("G3:G43").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(244, _
        176, 132)
   WS.Range("G3:G43").Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(184, _
        137, 219)
    WS.Range("G3:G43")).Sort.SortFields.Add(Range("B4:B43"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(155, _
        194, 230)
   WS.Range("G3:G43").Sort.SortFields.Add2 Key:=Range("G4:G43") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With WS.Range("G3:G43").Sort
        .SetRange Range("B3:J43")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B4:B43").Select
    WS.Range("G3:G43").Sort.SortFields.Clear
   WS.Range("G3:G43").Sort.SortFields.Add2 Key:=Range("B4:B43") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With WS.Range("G3:G43").Sort
        .SetRange Range("B4:B43")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("C4").Select
End Sub


Thank you very much
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi there. Give this worksheet loop a try.

VBA Code:
Sub A_Sort()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    With ws
        .Range("B4:J43").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(169, _
            208, 142)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(244, _
            176, 132)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(184, _
            137, 219)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(155, _
            194, 230)
        .Sort.SortFields.Add2 Key:=Range("G4:G43") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("B3:J43")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("B4:B43").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=Range("B4:B43") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("B4:B43")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("C4").Select
    End With
Next ws

End Sub
 
Upvote 0
Hi @breynolds0431
Thanks, I think it needs more adjustment to work

1673203845431.png
 
Upvote 0
I have another VBA code, and it's working very well, but for another purpose
Take a look please

VBA Code:
Sub SortActiveSheet()
    Dim ws As Worksheet

    Set ws = ActiveSheet

    With ws.Sort
        If Not ws.Range("C4:J43").MergeCells Then
            .SortFields.Clear
            .SortFields.Add2 Key:=ws.Range("G3:G43"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange ws.Range("C3:J43")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        Else
            MsgBox "Error - sort range contains merged cells", vbCritical, "Sorting worksheet: " & ws.Name
        End If
    End With
End Sub
 
Upvote 0
Hi @breynolds0431
Thanks, I think it needs more adjustment to work

View attachment 82311
Oh, sorry. Since you are selecting cells, which usually isn't necessary, you'd also need to select the sheet as you loop through them. Try the below.

VBA Code:
Sub A_Sort()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    With ws
        .Select
        .Range("B4:J43").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(169, _
            208, 142)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(244, _
            176, 132)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(184, _
            137, 219)
        .Sort.SortFields.Add(Range("B4:B43"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(155, _
            194, 230)
        .Sort.SortFields.Add2 Key:=Range("G4:G43") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("B3:J43")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("B4:B43").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=Range("B4:B43") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("B4:B43")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("C4").Select
    End With
Next ws

End Sub
 
Upvote 0
Solution
Would you be able to provide a little example, preferably using XL2BB or sharing on dropbox, of one of the sheets to format? If you want to just provide a template/shell without any sensitive data, that should be okay. Just trying to better understand what needs to be sorted on each sheet.
 
Upvote 0
Doesn't:

VBA Code:
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(169, 208, 142)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(244, 176, 132)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(184, 137, 219)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(155, 194, 230)

Just Equate to:
VBA Code:
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(155, 194, 230)
?
 
Upvote 0
Would you be able to provide a little example, preferably using XL2BB or sharing on dropbox, of one of the sheets to format? If you want to just provide a template/shell without any sensitive data, that should be okay. Just trying to better understand what needs to be sorted on each sheet.
test it.xlsm
ABCDEFGHIJK
1
2 SPAIN GMT+1 // BRAZIL GMT-3 M 04:00 - 10:00 | A 10:00 - 16:00 | E 16:00 - 22:00 | N 22:00 - 04:00
3FLIGHT #TRAVELLERFROMTOAIRLINETIMESTATUSCOMMENTS
40131. RebeccaBCNEZEIB0:30DATE CHANGED87.00 €
50247. RickMXPVIEOS9:15OK
60348. MarkMXPVIEOS9:15OK
70447. DaleVIEKIVOS11:40OK
80548. SmithVIEKIVOS11:40OK
90606. JohnJFKPUJB614:49OK
100790. WilliamCPTLHRBA16:40REFUNDED
110803. SandyEZELHRBA18:25REFUNDED
120903. ClaudeAEPGRUG322:35OK
131084. MichaleSJOMADIB23:50OK
1411
1512
1613
1714
1815
1916
2017
2118
2219
2320
2421
2522
2623
2724
2825
2926
3027
3128
3229
3330
3431
3532
3633
3734
3835
3936
4037
4138
4239
4340
44 TOTALS BUMPEDUSEDBOOKINGS
450710
Jan_4
Cell Formulas
RangeFormula
F45F45=COUNTIF(H4:H43,"X")
H45H45=COUNTIF(H4:H43,"OK")
I45I45=COUNTA(F4:F43)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
J4Expression=MOD(ROW(),2)=0textNO
J4Expression=K4:K28="X"textNO
J4Expression=K4:K43="X"textNO
J5:J6Expression=MOD(ROW(),2)=0textNO
J5:J6Expression=K5:K44="X"textNO
J7:J43Expression=MOD(ROW(),2)=0textNO
J29:J43Expression=K29:K52="X"textNO
J7:J28Expression=K7:K46="X"textNO
I4Expression=MOD(ROW(),2)=0textNO
I4Expression=H4:H28="X"textNO
I5:I43Expression=MOD(ROW(),2)=0textNO
I29:I43Expression=H29:H52="X"textNO
I5:I28Expression=H5:H29="X"textNO
G4Expression=MOD(ROW(),2)=0textNO
G4Expression=H4:H28="X"textNO
G4Expression=H4:H43="X"textNO
H4,C4:F4Expression=MOD(ROW(),2)=0textNO
C4Expression=H4:H28="X"textNO
D4Expression=H4:H28="X"textNO
E4Expression=H4:H28="X"textNO
F4Expression=H4:H28="X"textNO
H4Expression=H4:H28="X"textNO
C4Expression=H4:H43="X"textNO
G5:G6Expression=MOD(ROW(),2)=0textNO
G5:G6Expression=H5:H44="X"textNO
C7:H43,C5:F6,H5:H6Expression=MOD(ROW(),2)=0textNO
B4:B43Expression=AA4="N"textNO
B4:B43Expression=AA4="E"textNO
B4:B43Expression=AA4="A"textNO
B4:B43Expression=AA4="M"textNO
F29:F43Expression=H29:H52="X"textNO
H29:H43Expression=H29:H52="X"textNO
G29:G43Expression=H29:H52="X"textNO
E29:E43Expression=H29:H52="X"textNO
D29:D43Expression=H29:H52="X"textNO
C29:C43Expression=H29:H52="X"textNO
H5:H28Expression=H5:H44="X"textNO
G7:G28Expression=H7:H46="X"textNO
F5:F28Expression=H5:H44="X"textNO
E5:E28Expression=H5:H44="X"textNO
D5:D28Expression=H5:H44="X"textNO
C5:C28Expression=H5:H44="X"textNO
Cells with Data Validation
CellAllowCriteria
H4:H43List=Status_Day
I4:I43List=INDIRECT(H4)
 
Upvote 0
Doesn't:

VBA Code:
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(169, 208, 142)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(244, 176, 132)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(184, 137, 219)
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(155, 194, 230)

Just Equate to:
VBA Code:
    ActiveWorkbook.Worksheets("Jan_3").Sort.SortFields.Add(Range("B4:B43"), xlSortOnCellColor, xlAscending, _
            , xlSortNormal).SortOnValue.Color = RGB(155, 194, 230)
?
Got it, but I recorded this macro using Sort levels, how could I make the code recognize the four colors?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
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