Sort rows by colour

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi All

I would like to sort all rows with highlight to the bottom and I have used the macro recorder to create the macro below. However I would like to make this macro generic with unknown number of rows. Please could anyone help?

VBA Code:
Private Sub Highlight()


Columns("A:G").Select
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "C2:C36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "D2:D36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "G2:G36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Change Log").Sort
        .SetRange Range("A1:G36")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
IF you want to format code inside code tags then use <rich/>
Rich (BB code):
    With ActiveWorkbook.Worksheets("Change Log").Sort
        .SetRange Range("A1:G36")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

If you use anything else it gets messed up like this:
VBA Code:
    With ActiveWorkbook.Worksheets("Change Log").Sort
        .SetRange Range("A1:G36")
        .Header = xlYes
        .MatchCase = False
[FONT=trebuchet ms]   [COLOR=rgb(184, 49, 47)]     .Orientation = xlTopToBottom[/COLOR][/FONT]
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Thank you for the tips!

VBA Code:
Columns("A:G").Select
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "C2:C36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "D2:D36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Change Log").Sort.SortFields.Add2 Key:=Range( _
        "G2:G36"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Change Log").Sort
        .SetRange Range("A1:G36")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,539
Messages
6,120,100
Members
448,944
Latest member
SarahSomethingExcel100

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