Sort Range by color

nandobarreto

New Member
Joined
Jan 22, 2018
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a Table where I color some cells and sometimes I need to sort them by color.
To do so, I go on the sort menu.
I would like to do this using a VBA macro

I "wrote" this, but isnt working,

VBA Code:
Sub Macro2()
'
   Dim rng As String
    rng = Selection.Address

ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:= _
        Range(rng), SortOn:=xlSortOnCellColor, Order:=xlDescending, _
        DataOption:=xlSortNormal
    
With ActiveWorkbook.ActiveSheet.Sort
        .SetRange Range(rng)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

    End With 
End Sub

I tried too:

VBA Code:
Dim rng As Range
Set rng = Range(Selection.Address)

No lucky!
Any help?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Have a look at this
 
Upvote 0
In what way isn't it working?
Thats a good question, i forgot to mention!

When I select only 1 column it does what should
When I select 2 or more, gives me an error (i'll try translate):

The classification reference is not valid. Check if the reference is among the data you want to sort and that the first box to sort by is not the same or is blank.

Have a look at this

Thanks for your reply, but this classifies columns, I want to classify only SELECTED ranges on a sheet.
I looked this before post.
 
Upvote 0
How about
VBA Code:
Sub nandobarreto()
   Dim i As Long
   
   With ActiveSheet.Sort
      .SortFields.Clear
      For i = 1 To Selection.Columns.count
         .SortFields.Add Selection.Columns(i), xlSortOnCellColor, xlDescending, xlSortNormal
      Next i
      .SetRange Selection
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub nandobarreto()
   Dim i As Long
  
   With ActiveSheet.Sort
      .SortFields.Clear
      For i = 1 To Selection.Columns.count
         .SortFields.Add Selection.Columns(i), xlSortOnCellColor, xlDescending, xlSortNormal
      Next i
      .SetRange Selection
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub

Wow! worked perfectly!
1 more question:

Is it possible to first put colors on the top, second no colors and 3rd blank cells?

BTW, thank you so much!
 
Upvote 0
How about
VBA Code:
Sub nandobarreto()
   Dim i As Long
   
   With ActiveSheet.Sort
      .SortFields.Clear
      For i = 1 To Selection.Columns.count
         .SortFields.Add Selection.Columns(i), xlSortOnCellColor, xlDescending, xlSortNormal
      Next i
      For i = 1 To Selection.Columns.count
         .SortFields.Add Selection.Columns(i), xlSortOnValues, xlAscending, xlSortNormal
      Next i
      .SetRange Selection
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub
 
Upvote 0
Actually, if the first cell is colorless, it'll keep as first, and sort only after the second line.
And if I select the whole line it'll return an error:

application definition or object definition error

The first problem I tryed to change i=0 but wont work.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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