Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Aug 2004
    Posts
    361
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting based on background color (XP)

    The following comes from 179,114. A co-worker has given me a spreadsheet which she would like to sort based on rows that have been highlighted in one particular color. This spreadsheet has many columns of text. This code appears to be written for two columns based upon numeric information. How can I modify it to be used for a multicolumn worksheet? Or should I be trying to use Chip Pearson's page dealing with ColorIndex? Help would be greatly appreciated.

    Sub SortByColor()
    Dim c As Range, upLt As Range, lowRt As Range
    ActiveCell.Next.EntireColumn.Insert
    Set upLt = ActiveCell.End(xlUp)
    Set lowRt = ActiveCell.End(xlDown).Next

    For Each c In Range(upLt, lowRt.Previous)
    c.Next.Value = c.Interior.ColorIndex
    Next c
    Range(upLt, lowRt).Next.Sort _
    key1:=upLt.Next
    lowRt.EntireColumn.Delete
    End Sub

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Sorting based on background color (XP)

    I think the easiest way would be to use Chip's functions at SortByColor (for others who may want to find them) to get the colorindex and then sort on these columns.

    It is much simpler (and will sort faster) to use the built-in Sort with a function to create your own sort routine.

    Even if you wanted to it with code, I would recommend having the macro use the functions to create temp columns of colorindex values and then have the macro call the built-in sort routine.

    Steve

  3. #3
    3 Star Lounger
    Join Date
    Aug 2004
    Posts
    361
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sorting based on background color (XP)

    You are correct. Chip's function works extremely well and is quick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •