Evenflow
12-13-2005, 10:21 AM
Hello. Is there a way to sort by format? For example I have a spreadsheet with around 5,000 lines...some of which are highlighted in red. Is there a way to sort the highlighted lines to the top or bottom of the sheet? Thanks.
Newman
12-13-2005, 11:41 AM
A function that looks like this would add a cell with the number that represent the color of your cell. Then, you just have to sort that column.
(Note that this code isn't right. It is just a hint. You'll have to do some modification to make it work right.)
Public Function SortByColors()
Dim MyRange, Cell As Range
Set MyRange= Range("A1:A10")
For Each Cell In MyRange
Cell.Offset(0, 10).Value = Cell.Interior.ColorIndex
Next Cell
End function
I hope this could be fixed for you by someone who have more time than I do.
Brianwarnock
12-16-2005, 07:13 AM
Hi
I realise this is an old thread and you have probably solved your problem but if anybody else finds this via a search then they might like to try the following macro.
edit- Please note that a cells colourindex is not altered by conditional formatting.
Brian
Sub sortcolour()
'Brian Warnock December 2005
'Sort rows by colour index
Dim lnglastrow As Long
Dim lnglastcol As Long
Dim lnglastcol2 As Long
lnglastrow = ActiveSheet.UsedRange.Rows.Count
lnglastcol = ActiveSheet.UsedRange.Columns.Count
lnglastcol2 = lnglastcol + 1
For Each c In Range(Cells(1, lnglastcol), Cells(lnglastrow, lnglastcol))
c.Offset(0, 1).Value = c.Interior.ColorIndex
Next c
ActiveSheet.Range(Cells(1, 1), Cells(lnglastrow, lnglastcol2)).Sort _
key1:=ActiveSheet.Cells(1, lnglastcol2), order1:=xlDescending
ActiveSheet.Cells(1, lnglastcol2).EntireColumn.Delete
End Sub