Sub d_CellColor_and_Total_in_all_sheets_COUNT()
Dim i As Long, j As Long
Dim k As Integer, l As Integer
Dim LastRow As Long
Dim ws As Object
For Each ws In Worksheets
ws.Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("B1:B" & LastRow).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = LastRow To 1 Step -1
If Range("B" & i).Value >= 0 And Range("B" & i).Value <= 9 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 36
If Range("B" & i).Value >= 10 And Range("B" & i).Value <= 14 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 4
If Range("B" & i).Value >= 15 And Range("B" & i).Value < 20 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 44
If Range("B" & i).Value >= 20 And Range("B" & i).Value < 100 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 3
'unlikely to need following lines as the range is already sorted:
' Range("B1:B" & LastRow).Select
' Selection.Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlGuess, _
' OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
' DataOption1:=xlSortNormal
Next i
k = 3
For j = LastRow To 1 Step -1
If Range("B" & j).Interior.Color <> Range("B" & j + 1).Interior.Color Then
Range("B" & j + 1).Select
For l = 1 To k
Selection.EntireRow.Insert
Selection.EntireRow.Clear
Next l
End If
Next j
Range("2:4").Select
Selection.Delete Shift:=xltoup
Dim LR As Long
Dim Area As Range
With ActiveSheet
LR = .Range("J" & .Rows.Count).End(xlUp).Row
If LR = 2 Then
.Range("J4").Formula = "=SUM(J2)"
.Range("K4").Formula = "=COUNT(J2)"
.Range("K4").Font.ColorIndex = 2
'I'm guessing that B2 is the right cell to look at:
[COLOR=Red] .Range("A4").Value = Choose(Application.Match(.Range("B2").Value, Array(0, 10, 15, 20), 1), "a", "b", "c", "d")
.Range("A4").Interior.ColorIndex = .Range("B2").Interior.ColorIndex[/COLOR]
Else
For Each Area In .Range("J2:J" & LR).SpecialCells(xlCellTypeConstants).Areas
With Area.Resize(1).Offset(Area.Rows.Count + 1)
.Formula = "=SUM(" & Area.Address & ")"
.Offset(0, 1).Formula = "=COUNT(" & Area.Address & ")"
.Offset(0, 1).Font.ColorIndex = 2
[COLOR=Red] ActiveSheet.Cells(.Row, "A").Interior.ColorIndex = Area.Resize(1).Interior.ColorIndex
ActiveSheet.Cells(.Row, "A").Value = Choose(Application.Match(ActiveSheet.Cells(Area.Resize(1).Row, "B").Value, Array(0, 10, 15, 20), 1), "a", "b", "c", "d")[/COLOR]
End With
Next Area
End If
End With
Next
End Sub