VBA Question- Demand Analysis...How can I count activity by time of day?

WonkaCEO

New Member
Joined
Sep 3, 2015
Messages
4
Excel 2010
ABC
1Customer NumberIn Date-TimeOut Date-Time
219/3/2015 15:009/4/2015 15:36
329/3/2015 15:209/4/2015 9:45
439/5/2015 12:159/7/2015 15:59
549/5/2015 15:009/6/2015 15:03
659/7/2015 8:009/8/2015 18:09
769/7/2015 13:009/9/2015 13:36
879/10/2015 11:009/13/2015 16:52
989/10/2015 10:239/11/2015 7:44
1099/11/2015 7:009/12/2015 10:36
11109/11/2015 8:009/11/2015 13:52

<tbody>
</tbody>
Data

My data lists a series of customers leaving their cars for service. I'd like to develop VBA code that will count the number of cars that are present during each hour of the day.

I'm looking to create output that lists each hours of the day and the number of cars that were present during each hour. Any help would be greatly appreciated.

Here's a sample of the output:

Excel 2010
AB
1Hour of DayCount of Customers
200
301
402
503
604
705
806
907
1008
1109
1210
1311
1412
1513
1614
1715
1816
1917
2018
2119
2220
2321
2422
2523

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Output
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Are you sure the list should be 0 - 24? Should you not also have to add the date?


AB
1Hour of day:Count of customers:
29/3/2015 0:00=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A2;'DATA'!C:C;">="&'OUTPUT'!A2)
3=$A2+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A3;'DATA'!C:C;">="&'OUTPUT'!A3)
4=$A3+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A4;'DATA'!C:C;">="&'OUTPUT'!A4)
5=$A4+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A5;'DATA'!C:C;">="&'OUTPUT'!A5)
6=$A5+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A6;'DATA'!C:C;">="&'OUTPUT'!A6)
7etc

<tbody>
</tbody>



Because list including date would grow very large you might want to consider creating a field in which you type the date, and that the table is updated based on that field. For instance in below table the date would be entered in B1:


AB
1Input field for date9/3/2015
2
3Hour of day:Count of customers:
4=$B$1+0/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A4;'DATA'!C:C;">="&'OUTPUT'!A4)
5=$A4+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A5;'DATA'!C:C;">="&'OUTPUT'!A5)
6=$A5+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A6;'DATA'!C:C;">="&'OUTPUT'!A6)
7=$A6+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A7;'DATA'!C:C;">="&'OUTPUT'!A7)
8=$A7+1/24=COUNTIFS('DATA'!B:B;"<="&'OUTPUT'!A8;'DATA'!C:C;">="&'OUTPUT'!A8)
9etc

<tbody>
</tbody>




Edited to add row numbers and column headers to the tables
 
Last edited:
Upvote 0
Peter,

I am ultimately looking to calculate the average number of cars in each hour per day. My intent of the output was to add the number of occurrences in each hour and divide by the total number of days in the study. Currently, I am looking to do this for a period of 1 year.

Thanks again for the help.
 
Upvote 0
Ok, in that case I would need three helper columns on the source data. I would not be susprised if someone with more excel skills knows how to do it without helper columns (array formulas?), but here goes:


  • Add a helper column to determine the entry time by using formula =HOUR(B#)
  • Add a helper column to determine the departure time by using formula =HOUR(C#)
  • Add a helper column to determine if a customer overnighted more then once. So only full 24h days where the car was parked are counted! If a car left on the same day as he arrived the value is 0. If the car left the first day after arrival the value is 0. If the car left the second day after arrival the value is 1. If the car left the third day after arrival the value is 2. If the car left the fourth day after arrival the value is 3, and so on. This will show how many customers have to be added to each time window (hour).


ABCDEF
1Customer In Date-TimeOut Date-TimeEntry timeDeparture timeAmount of full (24h) days parked
219/3/2015 15:009/4/2015 15:36=HOUR(B2)=HOUR(C2)=IF(ROUNDDOWN(C2-B2;0)=0;0;ROUNDDOWN(C2-B2;0)-1)
329/3/2015 15:209/4/2015 9:45=HOUR(B3)=HOUR(C3)=IF(ROUNDDOWN(C3-B3;0)=0;0;ROUNDDOWN(C3-B3;0)-1)
439/5/2015 12:159/7/2015 15:59=HOUR(B4)=HOUR(C4)=IF(ROUNDDOWN(C4-B4;0)=0;0;ROUNDDOWN(C4-B4;0)-1)
549/5/2015 15:009/6/2015 15:03=HOUR(B5)=HOUR(C5)=IF(ROUNDDOWN(C5-B5;0)=0;0;ROUNDDOWN(C5-B5;0)-1)
659/7/2015 8:009/8/2015 18:09=HOUR(B6)=HOUR(C6)=IF(ROUNDDOWN(C6-B6;0)=0;0;ROUNDDOWN(C6-B6;0)-1)
769/7/2015 13:009/9/2015 13:36etcetcetc
879/10/2015 11:009/13/2015 16:52
989/10/2015 10:239/11/2015 7:44
1099/11/2015 7:009/12/2015 10:36
11109/11/2015 8:009/11/2015 13:52

<tbody>
</tbody>






The calculation of the output file works the same as in my first reply, except for two differences:


  • The hour of day is now just number 0-24, instead of based on date/time. So no more 0/24, 1/24, 2/24 etc.
  • To include the overnighting cars I have added a sum formula for all values in column F.


ABC
1Hour of DayCount of CustomersAverage per day
20=COUNTIFS(DATA!D:D;"<="&OUTPUT!A2;DATA!E:E;">="&OUTPUT!A2)+SUM(DATA!F:F)=B2/365
31=COUNTIFS(DATA!D:D;"<="&OUTPUT!A3;DATA!E:E;">="&OUTPUT!A3)+SUM(DATA!F:F)=B3/365
42=COUNTIFS(DATA!D:D;"<="&OUTPUT!A4;DATA!E:E;">="&OUTPUT!A4)+SUM(DATA!F:F)=B4/365
53=COUNTIFS(DATA!D:D;"<="&OUTPUT!A5;DATA!E:E;">="&OUTPUT!A5)+SUM(DATA!F:F)=B5/365
64=COUNTIFS(DATA!D:D;"<="&OUTPUT!A6;DATA!E:E;">="&OUTPUT!A6)+SUM(DATA!F:F)=B6/365
75etcetc
86
97
108
119
1210
1311
1412
1513
1614
1715
1816
1917
2018
2119
2220
2321
2422
2523

<tbody>
</tbody>
 
Last edited:
Upvote 0
Thanks again for the help. You are definitely helping me get closer to the solution. Unfortunately, I may not be communicating the logic clearly. I hope this helps explain it.

Here's a sample of five:

Excel 2010
ABCDEF
1Customer NumberIn Date-TimeOut Date-TimeEntry timeDeparture timeAmount of full (24h) days parked
219/3/2015 15:009/4/2015 15:3615150
329/3/2015 15:209/4/2015 9:451590
439/5/2015 12:159/7/2015 15:5912151
549/5/2015 15:009/6/2015 15:0315150
659/7/2015 8:009/8/2015 18:098180

<tbody>
</tbody>
Data



Here's how I manually calculated the average number of cars per day in each hour:

Excel 2010
HIJKLMNOPQRSTUVWXYZAAABACADAEAF
1Manual Count By Hour01234567891011121314151617181920212223
2Customer 1111111111111111211111111
3Customer 2111111111100000111111111
4Customer 3222222222222333322222222
5Customer 4111111111111111211111111
6Customer 5111111112222222222211111
7Total Occurrences6666666677667771077766666
8Total Days666666666666666666666666
9Avg Per Day1.001.001.001.001.001.001.001.001.171.171.001.001.171.171.171.671.171.171.171.001.001.001.001.00

<tbody>
</tbody>
Data
For example, Customer 1 entered in the 3 o'clock hour on Sept 3rd and left in the 3 o'clock hour on Sept 4th. When counting, the car was present each once during each hour and twice in the three o'clock hour during it's entire stay. My intent was to do this for each customer, then total the amount of occurrences in each hour and divide by the total number of days being reviewed.

I hope this helps clarify the logic. Thanks...
 
Upvote 0
I see what you mean. The calculation works perfectly when the timeslot you enter is earlier then the timeslot you leave. But when the departure timeslot lies before the arrival timeslot it does not work. In other words, an arrival today at 16:00 and a departure the next day at 17:00 works fine,but an arrival today at 16:00 and a departure the next day at 15:00 will mess up the results.

I have modified the formula. It will now calculate the amount of hours on the day of arrival, the amount of hours on the day of departure (if different from arrival date), and the amount of full days a car has been parked.

The formulas are getting a bit long now, so I won't be posting them in the table but list them below:


  • Column D
    =HOUR(B2),
    Simple formula to calculate the timeslot a car enters.
  • Column E,
    =IF(ROUNDDOWN(C2;0)=ROUNDDOWN(B2;0);HOUR(C2);23)
    This calculates when the car leaves on the first day. If a car does NOT leave the same day it will always show 23. The departure date is then calculated in column G.
  • Column F,
    =ROUNDDOWN(C2;0)-ROUNDDOWN(B2;0)-IF(ROUNDDOWN(C2;0)=ROUNDDOWN(B2;0);0;1)
    This column calculates the total amount of FULL days a car is in the garage.
  • Column G,
    =IF(ROUNDDOWN(B2;0)=ROUNDDOWN(C2;0);"";HOUR(C2))
    If a car does not leave on the same day it arrives it will show the timeslot it left. If a car leaves the same day it arrives it will show "".


ABCDEFG
1Customer NumberIn Date-TimeOut Date-TimeStart timeEnd time (arrival day)Full daysEnd time (dep. date)
219/3/2015 15:009/4/2015 15:36
329/3/2015 15:209/4/2015 9:45
439/5/2015 12:159/7/2015 15:59
549/5/2015 15:009/6/2015 15:03

<tbody>
</tbody>





On the output file you need this formula:
=COUNTIFS(DATA!D:D;"<="&OUTPUT!A2;DATA!E:E;">="&OUTPUT!A2)+SUM(DATA!F:F)+COUNTIFS(DATA!G:G;">="&OUTPUT!A2)
 
Upvote 0
It works!!! Thanks for helping me out with this. Your solution is much easier than the VBA I was attempting to write. Have a great holiday weekend!
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top