0
votes

I am trying to devise a non-vba Excel function which checks each cell in multiple ranges for different parameters and then returns the number of applicable occurrences. My specific problem occurs when applying the HOUR function to a range of date-cells and counting how many of these cells are equal to a certain hour.

To clarify, in VBA I would write something like (please assume that column A contains dates):

Sub tester33()
Dim r As Range
Dim c
Dim a As Integer
Set r = Range("A1:A10")
a = 0
For Each c In r
    If (Hour(c) = 5) Then 'time is 5 am, other if conditions are omitted
        a = a + 1
    End If
Next c
MsgBox (a)
End Sub

What I would like to have working is

=COUNTIF(HOUR(A:A);"=5")

My search so far returned nada. Please note that I found answers to this question here and here, that unfortunately won't work for nonmathematical Excel functions. I hope this isn't a completely trivial problem, as I am new to in-sheet formulas.

2

2 Answers

0
votes

An alternative (non-array) formula is to use

=SUMPRODUCT(--(HOUR(A:A)=5))

which doesn't require committing the formula using Ctrl+Shift+Enter. It's ultimately a matter of preference.

Also, if your data doesn't stretch the entire length of column A, consider using only the range occupied by data (e.g. A1:A50 instead of A:A), as Excel then doesn't have to iterate through blank cells, which can clunk things up.

0
votes

Use

=SUM((HOUR(A1:A50)=5)*1)

or

=SUM(--(HOUR(A1:A50)=5))

Both are array formulas so commit it by pressing Ctrl+Shift+Enter. Change A1:A50 to your data range.