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