Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel colour sort

Status
Not open for further replies.

faxof

Programmer
Dec 5, 2001
272
GB
hello

if i have a column of data, some cells are red, some are green. is there a way i can sort them by colour?

quizzed

fax
 
fax,

Not without VBA code.

What column has the shaded interior?


Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
it's colun A (the only column). it has people's names in their team colours. obviously i can sort it alphabetically but not by team colour.
 
Code:
Sub SortByInteriorColor()
   With ActiveSheet.UsedRange
      For Each s In Range(Cells(.Row, 1), Cells(.Row + .Rows.Count - 1, 1))
         With s
            Cells(.Row, "B").Value = .Interior.ColorIndex
         End With
      Next
   End With
   With ActiveSheet.UsedRange
      .Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlNo _
         , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
         DataOption1:=xlSortNormal
   End With
End Sub

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 

I had to do this a few months back and of course can't find it now, but I wrote a macro to place a value (Selection.Interior.ColorIndex) representing the color in a temporary column.

The phrase "sorting by color" is a little dubious. ie what is greater; Blue or Red. So by putting a the Color index next to the team, you can then group by Color (index)
 
that's good, thanks very much. however . . .

if i wanted to do it without code would something like this work:
put the data in A and formula in B, the pseudo formula in B1 would be something like =IF texcolour(A1)=red,"red","green"
then copy down
then sort on column B

could that work?

fax
 
Code:
Function WhatInteriorColor(rng)
   WhatInteriorColor = rng.Interior.ColorIndex
End Function

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
is it Background Shading or Font color? To my knowledge there is no excel function that reads either. That is why the answer lies in VB.
 
If the FONT is shaded
Code:
Function WhatFontColor(rng)
   WhatFontColor = rng.Font.ColorIndex
End Function
BTW,

The function must reside in a Module and NOT a Sheet OBject.

Skip,

[glasses] [red]Be advised:[/red] It's been reported that a wee psychic is roaming the countryside.
Small Medium @ Large! [tongue]
 
IIRC your cells can't have been coloured via Conditional formatting if the VBA code is to work, you need to have manually coloured them else the functions will not recognise the colour.

Regards
Ken..............

----------------------------------------------------------------------------
[peace]It's easier to beg forgiveness than ask permission[2thumbsup]

----------------------------------------------------------------------------
 
Actually another way to do it is have a column specifically for the Team.

Create a column for Team, and use conditional formatting to set the colour according to the Team selected. The column could be as wide or narrow as you like, depending on whether you want to be able to read the text. If you don't want to read the text, have the conditional formatting set the text colour the same colour as the cell background.

Now you can sort your data by Team, then Name - no code required [tongue]
 
There is an application on the market that does this.
I bought it for about GBP30 it will has many invaluable units such as filters, roll ups etc.
It's well worth checking out as I wouldn't be without it.


Regards, Phil.
C.E.O. Bodgeit, Leggit & Scarper. International.
"Stuffing things up completely since 1973"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top