View Single Post
  #17   Report Post  
Posted to microsoft.public.excel.programming
joeu2004 joeu2004 is offline
external usenet poster
 
Posts: 2,059
Default CountIf / SumProduct in VB

Errata...

On Jun 21, 8:24*am, joeu2004 wrote:
If you want to avoid VBA altogether, put the following
formula into M5 and copy down through M28:
=SUMPRODUCT(--(HOUR($F$4:$F$300)=ROWS($M$5:M5)-1))


Since you said that F4:F300 might not be fully populated, the
SUMPRODUCT formula should be:

=SUMPRODUCT(($F$4:$F$300<"")*(HOUR($F$4:$F$300)=R OWS($M$5:M5)-1))

I wrote regarding a VBA implementation:
There are better [ways] to implement it.


Here is one:

Sub countTimes()
'*** modify Consts as needed
Const srcRng As String = "f4" 'should be 1 cell
Const destRng As String = "m5:m28" 'should be 24 cells
'*** hr is two-dimensional to make it
'*** easier to write into Range(destRng)
Dim hr(0 To 23, 1 To 1) As Long, i As Long, x As Long
Dim data As Variant
'*** assume contiguous data starting in srcRng
If Not IsEmpty(Range(srcRng)) Then
'*** create data(1 to n, 1 to 1) as Variant
data = Range(srcRng, Range(srcRng).End(xlDown))
For i = 1 To UBound(data, 1)
x = Hour(data(i, 1))
hr(x, 1) = hr(x, 1) + 1
Next
End If
Range(destRng) = hr
End Sub