+ Reply to Thread
Results 1 to 9 of 9

sort cells by color ?Macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-15-2006
    Posts
    14

    Angry sort cells by color ?Macro?

    Hi everybody!

    I have a pretty messed up but COLORED table.
    Now I would like to put the CELLS WITHIN A ROW into the right order.

    Since each cell in a row has a specific color it's no problem - thats what I thought...!!! :-(

    Here's the scheme:
    Each row has 3-6 cells, all in a different color.
    Now, I would like to have these cells in a specific order: white, yellow, green, blue, black, red - so that I end up having in each column only one color.
    BUT in some rows some of those colors might be missing, that's why I actually donīt think a sorting function would really help. If a color is missing, then their should just be a blank in the colored column.

    Here's an example-file: http://www.herber.de/bbs/user/31944.xls

    I guess, it must be something like a macro that doesn't sort, but COPIES the colored cells of a row into the specified column [each color goes to an according column]
    so that it ends up with a column of red cells, a column of white cells, and so on...

    Here's an example-file: http://www.herber.de/bbs/user/31944.xls


    I am not good with VBA, I tried to record some macros and manipulate them, but I never reached anything usefull.

    Can anybody give me some hints??????

    THANKS SO MUCH FOR TAKING THE TIME!!!

  2. #2
    Peter Rooney
    Guest

    RE: sort cells by color ?Macro?

    Have you tried Jim Cone's Special Sort add-in?
    It can sort by background colour and font colour amongst many others - but,
    I think he likes to send it out himself.
    He's on [email protected] (at least that's what it says on the add-in help
    screen, so I hope he doesn't mind me telling you)

    Regards

    Pete

    "JVLennox" wrote:

    >
    > Hi everybody!
    >
    > I have a pretty messed up but COLORED table.
    > Now I would like to put the CELLS WITHIN A ROW into the right order.
    >
    > Since each cell in a row has a specific color it's no problem - thats
    > what I thought...!!! :-(
    >
    > Here's the scheme:
    > Each row has 3-6 cells, all in a different color.
    > Now, I would like to have these cells in a specific order: white,
    > yellow, green, blue, black, red - so that I end up having in each
    > column only one color.
    > BUT in some rows some of those colors might be missing, that's why I
    > actually donÂīt think a sorting function would really help. If a color
    > is missing, then their should just be a blank in the colored column.
    >
    > Here's an example-file: http://www.herber.de/bbs/user/31944.xls
    >
    > I guess, it must be something like a macro that doesn't sort, but
    > COPIES the colored cells of a row into the specified column [each color
    > goes to an according column]
    > so that it ends up with a column of red cells, a column of white cells,
    > and so on...
    >
    > Here's an example-file: http://www.herber.de/bbs/user/31944.xls
    >
    >
    > I am not good with VBA, I tried to record some macros and manipulate
    > them, but I never reached anything usefull.
    >
    > Can anybody give me some hints??????
    >
    > THANKS SO MUCH FOR TAKING THE TIME!!!
    >
    >
    > --
    > JVLennox
    > ------------------------------------------------------------------------
    > JVLennox's Profile: http://www.excelforum.com/member.php...o&userid=32505
    > View this thread: http://www.excelforum.com/showthread...hreadid=522938
    >
    >


  3. #3
    Ken Johnson
    Guest

    Re: sort cells by color ?Macro?

    Hi JVLennox,
    this worked for me, but where did that extra green cell come from?

    Public Sub ColorSort()
    Dim iLastRow As Long
    Dim iFirstRow As Long
    Dim iLastColumn As Long
    Dim iFirstColumn As Long
    Dim iRowCounter As Long
    Dim iColumnCounter As Integer
    Dim rgColorRange As Range
    Dim iWhiteColumns1 As Integer
    Dim iYellowColumns1 As Integer
    Dim iGreenColumns1 As Integer
    Dim iBlueColumns1 As Integer
    Dim iBrownColumns1 As Integer
    Dim iRedColumns1 As Integer
    Dim iWhiteColumns2 As Integer
    Dim iYellowColumns2 As Integer
    Dim iGreenColumns2 As Integer
    Dim iBlueColumns2 As Integer
    Dim iBrownColumns2 As Integer
    Dim iRedColumns2 As Integer
    Dim iWhitePaste As Integer
    Dim iYellowPaste As Integer
    Dim iGreenPaste As Integer
    Dim iBluePaste As Integer
    Dim iBrownPaste As Integer
    Dim iRedPaste As Integer
    Dim iFinalNumColumns As Integer
    Dim iLastWhiteCol As Integer
    Dim iLastYellowCol As Integer
    Dim iLastGreenCol As Integer
    Dim iLastBlueCol As Integer
    Dim iLastBrownCol As Integer
    Dim iLastRedCol As Integer

    Set rgColorRange =3D Application.InputBox( _
    Prompt:=3D"Please select the colored cells", _
    Default:=3DSelection.Address, _
    Type:=3D8)
    iFirstRow =3D rgColorRange.Row
    iLastRow =3D iFirstRow + rgColorRange.Rows.Count - 1
    iFirstColumn =3D rgColorRange.Column
    iLastColumn =3D iFirstColumn + rgColorRange.Columns.Count - 1
    For iRowCounter =3D iFirstRow To iLastRow
    iWhiteColumns1 =3D 0: iYellowColumns1 =3D 0: iGreenColumns1 =3D 0
    iBlueColumns1 =3D 0: iBrownColumns1 =3D 0: iRedColumns1 =3D 0
    For iColumnCounter =3D iFirstColumn To iLastColumn
    Select Case Cells(iRowCounter, iColumnCounter) _
    ..Interior.ColorIndex
    Case -4142
    If Cells(iRowCounter, iColumnCounter).Value <> "" Then
    iWhiteColumns1 =3D iWhiteColumns1 + 1
    End If
    Case 6
    iYellowColumns1 =3D iYellowColumns1 + 1
    Case 4
    iGreenColumns1 =3D iGreenColumns1 + 1
    Case 5
    iBlueColumns1 =3D iBlueColumns1 + 1
    Case 53
    iBrownColumns1 =3D iBrownColumns1 + 1
    Case 3
    iRedColumns1 =3D iRedColumns1 + 1
    End Select
    If iWhiteColumns1 > iWhiteColumns2 Then
    Let iWhiteColumns2 =3D iWhiteColumns1
    End If
    If iYellowColumns1 > iYellowColumns2 Then
    Let iYellowColumns2 =3D iYellowColumns1
    End If
    If iGreenColumns1 > iGreenColumns2 Then
    Let iGreenColumns2 =3D iGreenColumns1
    End If
    If iBlueColumns1 > iBlueColumns2 Then
    Let iBlueColumns2 =3D iBlueColumns1
    End If
    If iBrownColumns1 > iBrownColumns2 Then
    Let iBrownColumns2 =3D iBrownColumns1
    End If
    If iRedColumns1 > iRedColumns2 Then
    Let iRedColumns2 =3D iRedColumns1
    End If
    Next
    Next
    iLastWhiteCol =3D iFirstColumn + iWhiteColumns2
    iLastYellowCol =3D iLastWhiteCol + iYellowColumns2
    iLastGreenCol =3D iLastYellowCol + iGreenColumns2
    iLastBlueCol =3D iLastGreenCol + iBlueColumns2
    iLastBrownCol =3D iLastBlueCol + iBrownColumns2
    iLastRedCol =3D iLastBrownCol + iRedColumns2
    iFinalNumColumns =3D iLastRedCol _
    - iFirstColumn + 1
    For iRowCounter =3D iLastRow To iFirstRow Step -1
    With Range(Cells(iRowCounter, 1), _
    Cells(iRowCounter, iLastColumn))
    .Insert Shift:=3DxlDown
    .Offset(-1, 0).Clear
    End With
    iWhitePaste =3D 0: iYellowPaste =3D 0: iGreenPaste =3D 0
    iBluePaste =3D 0: iBrownPaste =3D 0: iRedPaste =3D 0
    For iColumnCounter =3D iFirstColumn To iLastColumn
    Select Case Cells(iRowCounter + 1, iColumnCounter) _
    ..Interior.ColorIndex
    Case -4142
    If Cells(iRowCounter + 1, iColumnCounter).Value <> "" Then
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iFirstColumn + iWhitePaste)
    iWhitePaste =3D iWhitePaste + 1
    End If
    Case 6
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iWhiteColumns2 + 1 + iYellowPaste)
    iYellowPaste =3D iYellowPaste + 1
    Case 4
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iWhiteColumns2 + iYellowColumns2 _
    + 1 + iGreenPaste)
    iGreenPaste =3D iGreenPaste + 1
    Case 5
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iWhiteColumns2 + iYellowColumns2 _
    + iGreenColumns2 + 1 + iBluePaste)
    iBluePaste =3D iBluePaste + 1
    Case 53
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iWhiteColumns2 + iYellowColumns2 _
    + iGreenColumns2 + iBlueColumns2 + 1 + iBrownPaste)
    iBrownPaste =3D iBrownPaste + 1
    Case 3
    Cells(iRowCounter + 1, iColumnCounter) _
    ..Copy Cells(iRowCounter, iWhiteColumns2 + iYellowColumns2 _
    + iGreenColumns2 + iBlueColumns2 + iBrownColumns2 + 1 _
    + iRedPaste)
    iRedPaste =3D iRedPaste + 1
    End Select
    Next
    Range(Cells(iRowCounter + 1, 1), _
    Cells(iRowCounter + 1, iLastColumn)).Delete Shift:=3DxlUp
    Next
    Range(Cells(iFirstRow, iFirstColumn), Cells(iFirstRow, _
    iLastColumn + iFinalNumColumns - 1)).Insert Shift:=3DxlDown
    Range(Cells(iFirstRow, iFirstColumn), _
    Cells(iFirstRow, iLastWhiteCol - 1)).Value =3D "WEISS"
    Range(Cells(iFirstRow, iLastWhiteCol), _
    Cells(iFirstRow, iLastYellowCol - 1)).Value =3D "GELB"
    Range(Cells(iFirstRow, iLastYellowCol), _
    Cells(iFirstRow, iLastGreenCol - 1)).Value =3D "GR=DCN"
    Range(Cells(iFirstRow, iLastGreenCol), _
    Cells(iFirstRow, iLastBlueCol - 1)).Value =3D "BLAU"
    Range(Cells(iFirstRow, iLastBlueCol), _
    Cells(iFirstRow, iLastBrownCol - 1)).Value =3D "BRAUN"
    Range(Cells(iFirstRow, iLastBrownCol), _
    Cells(iFirstRow, iLastRedCol - 1)).Value =3D "ROT"
    Range(Cells(iFirstRow, iFirstColumn), Cells(iFirstRow, _
    iLastRedCol)).Font.Bold =3D True
    End Sub

    Ken Johnson


  4. #4
    Registered User
    Join Date
    03-15-2006
    Posts
    14

    Red face

    Hey Ken, thanks a lot!

    BUT I always get a Syntax error in the Editor for the "3D" in your code...???
    Where does that come from?

    Thanks so much!

    @ Pete: I'll try sending him an email!

  5. #5
    Ken Johnson
    Guest

    Re: sort cells by color ?Macro?

    Hi JV,
    What is the "3D"?
    Which line produces the error?
    I copied the code from above then pasted it into a new workbook and it
    worked fine.
    Ken Johnson


  6. #6
    Tim Williams
    Guest

    Re: sort cells by color ?Macro?

    I think the added "3D" is a problem with some web-based newsgroup hosts
    (such as the one JV is using).

    Tim

    "Ken Johnson" <[email protected]> wrote in message
    news:[email protected]...
    > Hi JV,
    > What is the "3D"?
    > Which line produces the error?
    > I copied the code from above then pasted it into a new workbook and it
    > worked fine.
    > Ken Johnson
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1