I have the need to do a COUNTIF on three criteria with the third being across a range of columns! Can't think of a simple way to do this using a formula so I'm attempting to write a UDF.

The below is a breakdown of what I've attempted to write in the UDF along with a sample image of the data I'm working with...

- If the start time matches the time in the Start Column
- and the end time is less than or equal to the time in the End Column
- then count how many tasks match in that row
- repeat for each row in the table/range

- 11 matches for 'Task A'
- 12 matches for 'Task B'
- 4 matches for 'Task C'

This is the code I have for the UDF so far. It works up to the point of where I have the COUNTTASK = COUNTIF part. If I remove this and just say COUNTTASK = 1 then the formula returns 1 for the examples where the times match.

VBA Code:

```
Function COUNTTASK(Start_Time_Value, _
End_Time_Value, _
Task_Code_Value, _
Start_Time_Range As Range, _
End_Time_Range As Range, _
Task_Range As Range)
Dim StaRange()
Dim EndRange()
Dim TaskRange()
Dim i As Long
StaRange = Start_Time_Range
EndRange = End_Time_Range
TaskRange = Task_Range
For i = 1 To UBound(StaRange)
If StaRange(i, 1) = Start_Time_Value Then
If EndRange(i, 1) = End_Time_Value Then
COUNTTASK = WorksheetFunction.CountIf(TaskRange(i, 1), Task_Code_Value)
End If
End If
Next
End Function
```

Thanks in advance for any assistance with this.

Steven