السلام عليكم
جزاك الله خيرا استاذ ياسر هذا هو المطلوب
وقد قمت بتعديله ليلبي احتياجاتي بشكل اكبر فما رأيك
Sub SortColumnsByColorCount()
Dim iCol As Long
Dim firstRow As Long
Dim firstcol As Long
Dim lastRow As Long
Dim LastCol As Long
Dim i As Long
Dim x As Long
Application.ScreenUpdating = False
firstRow = 1
firstcol = 1
lastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row + firstRow - 1
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
For iCol = 1 To LastCol
Cells(1, iCol).Value = ColorFunction(Range(Cells(1, iCol), Cells(lastRow, iCol)))
Next iCol
Range(Cells(firstRow, firstcol), Cells(lastRow, LastCol)).Sort Key1:=Range("A" & 1), Header:=xlNo, Orientation:=xlLeftToRight
Range(Cells(firstRow, firstcol), Cells(firstRow, LastCol)).ClearContents
Application.ScreenUpdating = True
End Sub
Function ColorFunction(rRange As Range)
Dim rCell As Range
Dim vResult As Long
For Each rCell In rRange
If rCell.Interior.ColorIndex <> -4142 Then
vResult = vResult + 1
End If
Next rCell
ColorFunction = vResult
End Function