View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
David David is offline
external usenet poster
 
Posts: 1,560
Default Selecting cells of a specific color only.

Chip,

Appreciate your fast response however I was hoping there was a way to do
this procedure by using the existing Excel menus or if you explain to me how
to use this code. I am a novice when it comes to programing.

"Chip Pearson" wrote:

David,

You can use the code below to do this. The function RangeOfColor returns a
range containing the cells of the OfRange parameter which have a ColorIndex
equal to the ColorIndex parameter. If the OfText parameter is omitted or
False, the background fill color is tested. If OfText is True, the font
color of each cell is tested. You can then call this function with code
similar to the SelectRangeOfColor procedure below.

Function RangeOfColor(OfRange As Range, _
ColorIndex As Long, Optional OfText As Boolean) As Range
Dim ResRange As Range
Dim R As Range

For Each R In OfRange.Cells
If OfText = True Then
If R.Font.ColorIndex = ColorIndex Then
If ResRange Is Nothing Then
Set ResRange = R
Else
Set ResRange = Application.Union(ResRange, R)
End If
End If
Else
If R.Interior.ColorIndex = ColorIndex Then
If ResRange Is Nothing Then
Set ResRange = R
Else
Set ResRange = Application.Union(ResRange, R)
End If
End If
End If
Next R
Set RangeOfColor = ResRange

End Function

Sub SelectRangeOfColor()

Dim RR As Range
Dim ColorIndex As Long
Dim OfText As Boolean

ColorIndex = 3 ' RED. See help on ColorIndex for other values
OfText = False ' FALSE - test fill color, TRUE - test font color

If TypeOf Selection Is Excel.Range Then
If Selection.Cells.Count = 1 Then
MsgBox "Select the range to test.", vbOKOnly
Else
Set RR = RangeOfColor(Selection, ColorIndex, OfText)
If Not RR Is Nothing Then
RR.Select
End If
End If
Else
MsgBox "Select a range of cells and try again", vbOKOnly
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



"David" wrote in message
...
Is there a way to find and select cells only of a certain color in order
to
speed the formating of these cells. Any help would be appreciated.

Thanks David