Sort by selection

overbet

Board Regular
Joined
Jul 9, 2010
Messages
63
Office Version
  1. 2010
Will someone please help me with this? I have been trying for a couple weeks on and off and I keep giving up out of frustration. I am trying to sort by column T, by cells colors for the rows I have selected which changes. I recorded a macro and tried applying solutions from similar questions Ive read while looking for a solution. I know this has been asked many times, but I just cant get it to work for my criteria. This is the recorded macro with the sorting criteria I need followed by the closest solution/explanation I have found.

Code:
Sub Sort_Selection()


    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(79, 129 _
        , 189)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(153, _
        255, 204)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(153, 0 _
        , 204)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 255 _
        , 0)
    With ActiveWorkbook.Worksheets("Live").Sort
        .SetRange Range("A419:AE452")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Code:
Public Sub sSortSelection()


'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(1), Order:=xlDescending
    .SetRange Selection
    .Apply
End With


End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Please can anybody help me with this?

Will someone please help me with this? I have been trying for a couple weeks on and off and I keep giving up out of frustration. I am trying to sort by column T, by cells colors for the rows I have selected which changes. I recorded a macro and tried applying solutions from similar questions Ive read while looking for a solution. I know this has been asked many times, but I just cant get it to work for my criteria. This is the recorded macro with the sorting criteria I need followed by the closest solution/explanation I have found.

Code:
Sub Sort_Selection()


    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(79, 129 _
        , 189)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(153, _
        255, 204)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(153, 0 _
        , 204)
    ActiveWorkbook.Worksheets("Live").Sort.SortFields.Add(Range("T419:T452"), _
        xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 255 _
        , 0)
    With ActiveWorkbook.Worksheets("Live").Sort
        .SetRange Range("A419:AE452")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Code:
Public Sub sSortSelection()


'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(1), Order:=xlDescending
    .SetRange Selection
    .Apply
End With


End Sub
 
Upvote 0
If I understood correctly, you want to select several rows and sort that selection by the colors you have in column T.
Try the following.


Code:
Sub Sort_Selection_1()
    Dim rng As Range, rngT As Range, ini As Long, fin As Long
    
    Set rng = Selection
    
    ini = rng.Cells(1, 1).Row
    fin = rng.Rows.Count + ini - 1
    Set rngT = Range("T" & ini & ":T" & fin)
    With ActiveWorkbook.Worksheets("Live").Sort
        
        .SortFields.Clear
        .SortFields.Add(rngT, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(79, 129, 189)
        .SortFields.Add(rngT, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(153, 255, 204)
        .SortFields.Add(rngT, xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(153, 0, 204)
        .SortFields.Add(rngT, xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 255, 0)
        
        .SetRange Range("A" & ini & ":AE" & fin)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Wow that is exactly what Ive been trying to get. I cant thank you enough for your help and taking the time. I really have been trying for a few weeks and just couldnt get it. Many thanks
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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