問題は、色が赤ではないすべてのセルのカウントを減らすのではなく、再評価していることだと思います。
For Each Cell In colorD
If Cell.Interior.Color <> 3 Then
'Here you are re-evaluating F, not incrementing it.
F = Application.WorksheetFunction.CountIf(Range("C2:C" & F), "F")
'And the same for M.
M = Application.WorksheetFunction.CountIf(Range("C2:C" & M), "M")
End If
Next Cell
私はあなたのカウントを一度だけ評価し、赤血球を別々に追跡します(必要に応じてカウントからそれらを減らします):
Private Sub Workbook_Open()
Dim endRow As Long
Dim redF As Long
Dim redM As Long
Dim F As Long
Dim M As Long
Dim colorD As Range
Dim Cell As Range
Dim cellVal As String
'Find the ending row
endRow = Range("C" & Rows.Count).End(xlUp).endRow
'Ensure ending row is at least Row 2
If endRow < 2 Then
endRow = 2
End If
'Count all the Females
F = Application.WorksheetFunction.CountIf(Range("C2:C" & endRow), "F")
'Count all the Males
M = Application.WorksheetFunction.CountIf(Range("C2:C" & endRow), "M")
'Set the applicable Column D range
Set colorD = Range("D2", Range("D" & Rows.Count).End(xlUp))
'Loop through each cell in Column D
For Each Cell In colorD
If Cell.Interior.ColorIndex = 3 Then
'Red Cell found, get the cell value from Column C
cellVal = LCase(Cell.Offset(-1, 0).Value)
If cellVal = "f" Then redF = redF + 1 'Increment count of red Females
If cellVal = "m" Then redM = redM + 1 'Increment count of red Males
End If
Next Cell
'Subtract any red Females
F = F - redF
'Subtract any red Males
M = M = redM
'Alert User with counts
MsgBox ("Females=" & F & "," & "Males=" & M)
End Sub