Tweaked the code a little but and created a sample
Option Explicit
Public Sub fCountONSumTIME()
Dim lastRow As Long
Dim prevW As String
Dim prevD As Double
Dim countON As Long
Dim startTime As Double, _
endTime As Double, _
addTime As Double, _
sumTIME As Double
Dim r As Long
lastRow = Cells(65536, 23).End(xlUp).Row
prevW = "OFF" 'Set default ON/OFF state to start with
prevD = 0
countON = 0
sumTIME = 0
For r = 7 To lastRow
'Default Cell Formats
Cells(r, 4).Font.Bold = False
Cells(r, 4).Font.Color = vbBlack
Cells(r, 23).Font.Bold = False
Cells(r, 23).Font.Color = vbBlack
If (prevW = "OFF") And (Cells(r, 23) = "ON") Then
countON = countON + 1
startTime = Cells(r, 4)
'Start of ON (Bold RED Font)
Cells(r, 4).Font.Bold = True
Cells(r, 4).Font.Color = vbRed
Cells(r, 23).Font.Bold = True
Cells(r, 23).Font.Color = vbRed
End If
If (r <> 7) And (prevW <> "OFF") And (Cells(r, 23) = "OFF") Then
endTime = Cells(r, 4)
addTime = ((endTime - startTime) * 60 * 60 * 24)
sumTIME = sumTIME + addTime
'End of ON (Bold BLUE Font)
Cells(r, 4).Font.Bold = True
Cells(r, 4).Font.Color = vbBlue
Cells(r, 23).Font.Bold = True
Cells(r, 23).Font.Color = vbBlue
End If
'If current ON/OFF cell is an empty string then
'leave previous ON/OFF state to the last ON/OFF found.
If Not Cells(r, 23) = "" Then prevW = Cells(r, 23)
Next r
MsgBox "countON = " & countON
MsgBox "sumTime = " & sumTIME
'Place count of values in D5 & D6
Range("D5") = countON
Range("D6") = sumTIME
End Sub
Hope that helps!