Question

I am trying to count the instances of an event for both days of the week and hours of the day (to be graphed separately) with VBA. The date/time stamp is in this format: "2/28/2014 20:32"

Example:
AppID = 14329
DateCreated = 2/28/14 20:55
Day = Friday
Hour = 20:55

What I have going on right now is copying the data to a new column and setting the date format to "dddd" so it would display as "Sunday" etc. From there I am autofiltering for each day of the week and counting the resulting visible cells. This is working with this code:

With Range("C2:C" & LR)
                .AutoFilter

                .AutoFilter Field:=3, Criteria1:="Sunday"
                        cCnt = ActiveSheet.AutoFilter.Range.Columns(3) _
                        .SpecialCells(xlCellTypeVisible).Cells.Count
                            For Each cell In Range("C2:C" & cCnt)
                            Sun = Sun + 1
                            Next

                .AutoFilter Field:=3, Criteria1:="Monday"
                        cCnt = ActiveSheet.AutoFilter.Range.Columns(3) _
                        .SpecialCells(xlCellTypeVisible).Cells.Count
                            For Each cell In Range("C2:C" & cCnt)
                            Mon = Mon + 1
                            Next

...etc.

However, my issue is counting the events by hour. I am trying to do it similarly by copying the date/time to a new column and setting the format to "h:mm;@" and autofiltering by hour with this code:

With Range("D2:D" & LR)
            .AutoFilter

            .AutoFilter Field:=4, Criteria1:="10:??"
                        cCnt = ActiveSheet.AutoFilter.Range.Columns(4) _
                        .SpecialCells(xlCellTypeVisible).Cells.Count
                            For Each cell In Range("D2:D" & cCnt)
                            time(10) = time(10) + 1
                            Next

...etc.

This isn't working and I can't figure out why. I've tried changing the criteria1 to various syntax, adding a second criteria parameter and am stumped why this method would work for the first go around but not the second when both columns are derived from the same original data. I have also tried changing the data to a different time format (just "hh" etc).

Was it helpful?

Solution

How Excel stores Dates & Times:

Excel uses a 'serial' number system to store dates (first day is January 1, 1900):

01/01/1900 = 1

01/02/1900 = 2

Time is represented by anything after the decimal (noon is .5 because it's halfway through the day):

01/01/1900 12:00 = 1.5

01/02/1900 15:00 = 2.625

If you filter based on values, it's important to be aware of this.


Include this code in your module:

    Public Function GetDay(ByVal inputDate As String) As String

        GetDay = WeekdayName(Weekday(inputDate))

    End Function


    Public Function GetHour(ByVal inputDate As Range) As String
        
        GetHour = Format(inputDate.Value, "Medium Time")

    End Function

Now use these functions on your worksheet and AutoFill:

Raw Data Table Table Step 1 Table Step 2 Final Table

In your existing code modify the following:

    With Range("D2")

        .AutoFilter Field:=4, Criteria1:="10:?? PM"

            'If criteria didn't match anything, cCnt is equal to 1 (because header rows are visible and counted)
            cCnt = ActiveSheet.AutoFilter.Range.Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count

            If cCnt > 1 Then
            
                For Each cell In Range("D2:D" & cCnt)
                    Hour10PM = Hour10PM + 1
                Next
            
            End If

            MsgBox (Hour10PM & " matches for 10:00PM to 10:59PM")
            
            
        .AutoFilter Field:=4, Criteria1:="11:?? PM"

            cCnt = ActiveSheet.AutoFilter.Range.Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count

            If cCnt > 1 Then
            
                For Each cell In Range("D2:D" & cCnt)
                    Hour11PM = Hour11PM + 1
                Next
                
            End If
            
            MsgBox (Hour11PM & " matches for 11:00PM to 11:59PM")
            
    End With

Additional Notes:

Obviously I just gave a snippet of code, but the idea would be that you could split and keep count of every hour. Then to graph it should be simple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top