I'm trying to write a code to count the number of instances where a certain date AND a particular string occurs together in a database. I've written the following For Loop. However, the output for "JanHousekeeping" returns a value of 15.
The set of sample data that I am using looks like this.
I have 3 instances of "Housekeeping and other Hazards" but the loop returns a value of 15. Why is this and how could I fix this? Thank you in advance for the help!
Sub SummarySync()
JanHousekeeping = 0
FebHousekeeping = 0
MarHousekeeping = 0
AprHousekeeping = 0
MayHousekeeping = 0
JunHousekeeping = 0
JulHousekeeping = 0
AugHousekeeping = 0
SepHousekeeping = 0
OctHousekeeping = 0
NovHousekeeping = 0
DecHousekeeping = 0
For i = 5 To 20
For j = 5 To 20
DateCheckLoop = Worksheets("Jan").Cells(i, 4)
ObsCheck = Worksheets("Jan").Cells(j, 9).Value
If Month(DateCheckLoop) = 1 And ObsCheck = "Housekeeping and Other Hazards" Then
JanHousekeeping = JanHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 2 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
FebHousekeeping = FebHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 3 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
MarHousekeeping = MarHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 4 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
AprHousekeeping = AprHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 5 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
MayHousekeeping = MayHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 6 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
JunHousekeeping = JunHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 7 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
JulHousekeeping = JulHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 8 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
AugHousekeeping = AugHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 9 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
SepHousekeeping = SepHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 10 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
OctHousekeeping = OctHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 11 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
NovHousekeeping = NovHousekeeping + 1
ElseIf Month(Worksheets("Jan").Cells(i, 4)) = 12 And Worksheets("Jan").Cells(j, 9).Value = "Housekeeping and Other Hazards" Then
DecHousekeeping = DecHousekeeping + 1
Else
End If
Next j
Next i
Sheets("Site Visit Summary").Range("DateJan").Offset(1, 0) = JanHousekeeping
End Sub


ifstatement istruewhenj=5then it will count every time your outeriloop increments andjgoes back to 5 thus giving you a total of 15. It appears your double loop is flawed as you are cycling through rows for bothiandj=SUMPRODUCT((MONTH(D5:D20)=1)*(I5:I20="Housekeeping and Other Hazards"))