View Single Post
  #2  
Old June 2nd, 2010, 08:22 PM posted to microsoft.public.excel.misc
Project Mangler
external usenet poster
 
Posts: 6
Default VBA sort range with certain fontcolor

Hi Jack,

Option Explicit

Sub colorCell()
Dim A As Range
Dim B As Range
Dim c As Range
Dim D As Range

Set A = Cells(1, 11).End(xlDown)
Set B = Cells(Rows.Count, 11).End(xlUp)

For Each c In Range(A, B)
If c.Font.ColorIndex = 5 And D Is Nothing Then
Set D = c
ElseIf c.Font.ColorIndex = 5 And Not D Is Nothing Then
Set D = Application.Union(Range(D.Address), Range(c.Address))
End If
Next c
If D Is Nothing Then Exit Sub
Worksheets("Sheet1").Range(D.Offset(0, -10), D.Offset(0, 37)).Sort _
Key1:=Worksheets("Sheet1").Columns("K"), order1:=xlDescending, _
Header:=xlNo

End Sub


"Jack Sons" wrote in message
...
Hi all,

Ik column K there will be a continuous range of which all cells have a
certain fontcolor, the font color code is, say, 5. The range can be 1 cell

up
to 30 (or so), perhaps even zero. There are no cells in column K outside

that
range that also have font color code 5.
While executing the code it is not (yet) known what the rownumbers of the
first and last cells in that range are.

I need code that will sort (descending) the following range:

from row of first cell (in column K) with font color code 5 and column A
to
row of last cell (in column K) with font color code 5 and column AV. Of
course no sorting if none of the cells in column K have font color code 5.

I can't figure out the code, so I need your assistance, which will be
appreciated very much.

--
Jack Sons
The Netherlands