Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sort/Filter by Color

Is there any code written or any way that anyone knows of
to sort or filter based on the font or fill color?

Thanks,
Dan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Sort/Filter by Color

Hi Dan
to do this you need a custom function which would return the colorindex
of your cell in a helper column. Find below such a function:


To get the colorindex of a specific cell use
=ColorIndex(A1)
and copy this down. Now use column B as sorting/filtering criteria


------
'Code to paste in one of your modules

'---------------------------------------------------------------------
Function ColorIndex(rng As Range, _
Optional text As Boolean = False) As Variant
'---------------------------------------------------------------------
' Function: Returns the colorindex of the supplied range
' Synopsis:
' Author: Bob Phillips/Harlan Grove
'
'---------------------------------------------------------------------
Dim cell As Range, row As Range
Dim i As Long, j As Long
Dim iWhite As Long, iBlack As Long
Dim aryColours As Variant

If rng.Areas.Count 1 Then
ColorIndex = CVErr(xlErrValue)
Exit Function
End If

iWhite = WhiteColorindex(rng.Worksheet.Parent)
iBlack = BlackColorindex(rng.Worksheet.Parent)

If rng.Cells.Count = 1 Then
If text Then
aryColours = DecodeColorIndex(rng, True, iBlack)
Else
aryColours = DecodeColorIndex(rng, False, iWhite)
End If

Else
aryColours = rng.Value
i = 0

For Each row In rng.Rows
i = i + 1
j = 0

For Each cell In row.Cells
j = j + 1

If text Then
aryColours(i, j) = _
DecodeColorIndex(cell,True,iBlack)
Else
aryColours(i, j) = _
DecodeColorIndex(cell,False,iWhite)
End If

Next cell

Next row

End If

ColorIndex = aryColours

End Function

Private Function WhiteColorindex(oWB As Workbook)
Dim iPalette As Long
WhiteColorindex = 0
For iPalette = 1 To 56
If oWB.Colors(iPalette) = &HFFFFFF Then
WhiteColorindex = iPalette
Exit Function
End If
Next iPalette
End Function

Private Function BlackColorindex(oWB As Workbook)
Dim iPalette As Long
BlackColorindex = 0
For iPalette = 1 To 56
If oWB.Colors(iPalette) = &H0 Then
BlackColorindex = iPalette
Exit Function
End If
Next iPalette
End Function

Private Function DecodeColorIndex(rng As Range, text As Boolean, idx As
_
Long)
Dim iColor As Long
If text Then
iColor = rng.font.ColorIndex
Else
iColor = rng.Interior.ColorIndex
End If
If iColor < 0 Then
iColor = idx
End If
DecodeColorIndex = iColor
End Function



--
Regards
Frank Kabel
Frankfurt, Germany


Dan wrote:
Is there any code written or any way that anyone knows of
to sort or filter based on the font or fill color?

Thanks,
Dan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Sort/Filter by Color

Dan,

You need a helper column with a UDF that calculates the colour, and sort on
that

Her is a sample UDF

Function ColorIndex(rng As Range, Optional Text as Boolean = False))
If Text Then
ColorIndex = rng.Font.ColorIndex
Else
ColorIndex = rng.Interior.ColorIndex
End If
End Function

Use like
=ColorIndex(A1) for the cell colour or
=ColorIndex(A1, True) for the text colour

and then sort by that helper column.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Dan" wrote in message
...
Is there any code written or any way that anyone knows of
to sort or filter based on the font or fill color?

Thanks,
Dan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Sort/Filter by Color

Hi Bob
and I posted yours (and Harlans) full colorindex function and you just
gave the simplest version to the OP <vbg


--
Regards
Frank Kabel
Frankfurt, Germany


Bob Phillips wrote:
Dan,

You need a helper column with a UDF that calculates the colour, and
sort on that

Her is a sample UDF

Function ColorIndex(rng As Range, Optional Text as Boolean = False))
If Text Then
ColorIndex = rng.Font.ColorIndex
Else
ColorIndex = rng.Interior.ColorIndex
End If
End Function

Use like
=ColorIndex(A1) for the cell colour or
=ColorIndex(A1, True) for the text colour

and then sort by that helper column.


"Dan" wrote in message
...
Is there any code written or any way that anyone knows of
to sort or filter based on the font or fill color?

Thanks,
Dan


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Sort/Filter by Color

Hi Frank,

Keep posting it, it's good<vbg.

That function is better than my short version, in that it handles black and
white colours better than the short version, and it handles a multi-cell
range, which the short version doesn't. In the helper column, the multi-cell
range is not needed, but the black and white is probably useful still.

I need to produce a cut-down version to assist with sorting (actually I have
an add-in that automatically sorts by colour, names, IP addresses, and can
also do worksheets (custom or alphabetically), so it never seemed necessary.
Another project for the list.

Bob

"Frank Kabel" wrote in message
...
Hi Bob
and I posted yours (and Harlans) full colorindex function and you just
gave the simplest version to the OP <vbg


--
Regards
Frank Kabel
Frankfurt, Germany






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Sort/Filter by Color

Bob Phillips wrote:
Hi Frank,

Keep posting it, it's good<vbg.


Looking at the authors - no wonder <ebg



I need to produce a cut-down version to assist with sorting (actually
I have an add-in that automatically sorts by colour, names, IP
addresses, and can also do worksheets (custom or alphabetically), so
it never seemed necessary. Another project for the list.


What Add-in? (available somethere)

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Sort/Filter by Color

Only by mail, I don't have a public website (yet!).

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank Kabel" wrote in message
...
Bob Phillips wrote:
Hi Frank,

Keep posting it, it's good<vbg.


Looking at the authors - no wonder <ebg



I need to produce a cut-down version to assist with sorting (actually
I have an add-in that automatically sorts by colour, names, IP
addresses, and can also do worksheets (custom or alphabetically), so
it never seemed necessary. Another project for the list.


What Add-in? (available somethere)



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort by color: Is there an easy way to sort columns or rows in EX MGP Excel Worksheet Functions 5 August 16th 08 11:28 AM
Is it possible to sort or filter data by color of the field? Anna Excel Worksheet Functions 1 October 25th 06 04:21 PM
Sort or sub-total by Fill color or font color Excel_seek_help Excel Discussion (Misc queries) 1 April 27th 06 09:01 PM
Excel sort by Fill Color by custom list sort Dash4Cash Excel Discussion (Misc queries) 2 July 29th 05 10:45 PM
Is it possible to sort and/or filter by cell shading color eddie Excel Discussion (Misc queries) 3 February 28th 05 11:07 PM


All times are GMT +1. The time now is 03:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"