Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting Times within a Range.

Counting Times within a Range

by Allen Wyatt
(last updated July 8, 2017)

8

William has a list of times in column A. He needs a way to find how many of the times fall within a time range, such as between 8:30 am and 9:00 am. He tried using COUNTIF and a few other functions, but couldn't get the formulas to work right.

There are actually a few different ways you can count the times within the desired range, including using the COUNTIF function. In fact, here are two different ways you could construct the formula using COUNTIF:

=COUNTIF(A1:A100,">="&TIME(8,30,0))-COUNTIF(A1:A100,">"&TIME(9,0,0))
=COUNTIF(A1:A100,">=08:30")-COUNTIF(A1:A100,">09:00")

Either one will work fine; they only differ in how the starting and ending times for the range are specified. The key to the formulas is to grab a count of the times that are greater than the earliest boundary of the range and then subtract from that the times that are greater than the upper boundary.

You can shorten the formula by using the COUNTIFS function instead:

=COUNTIFS(A1:A100,">=8:30",A1:A100,"<=9:00")

You could also use the SUMPRODUCT function to get the desired result, in this manner:

=SUMPRODUCT((A1:A100>=8.5/24) * (A1:A100<=9/24))

This approach only works if the values in the range A1:A100 contain only time values. If there are dates stored in the cells as well, then it may not work because of the way that Excel stores dates internally. If the range does include dates, then you need to modify the formula to take that into account:

=SUMPRODUCT((ROUND(MOD(A1:A100,1),10)>=8.5/24) * (ROUND(MOD(A1:A100,1),10)<=9/24))

Finally, you could skip formulas altogether and use Excel's filtering capabilities. Apply a custom filter and you can specify that you only want times within the range you need. These are then displayed and you can easily count the results.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12398) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Counting Times within a Range.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Comma-Delimited Differences for PC and Mac

When you choose to save worksheet data in CSV format, Excel gives you three choices for file formats. Those choices are ...

Discover More

Displaying a Hidden First Row

If you hide the first rows of a worksheet, you may have a hard time getting those rows visible again. Here's a simple way ...

Discover More

What is a Desktop.ini File?

When browsing through your system, you might see several files named Desktop.ini. This tip explains what these files are ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Calculating TV Time

In some industries it is necessary to work with time resolutions of less than a second. If you need to keep track of such ...

Discover More

Dealing with Large Numbers of Seconds

When adding values to a time to calculate a new time, you may naturally choose to use the TIME function. This can cause ...

Discover More

Converting UTC Times to Local Times

Dates and times are often standardized on UTC time, which is analogous to GMT times. How to convert such times to your ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 + 2?

2018-03-04 15:50:12

John

@ Anita
You need to ensure all your "Time in Shift" dates are Excel dates.
The following dates "mix" 12 hour & 24 hour format by adding "PM".
2/15/2018 14:22 PM
2/15/2018 14:24 PM
2/15/2018 14:31 PM
2/15/2018 14:31 PM
2/15/2018 14:32 PM
2/15/2018 14:32 PM
2/15/2018 14:34 PM
2/15/2018 14:34 PM
When pasting (into Excel) those dates, Excel handles them as text.
Either remove the "PM" or convert the 24 hour format to 12 hour format.

Next separate the date and time into two columns.
Follow website https://www.extendoffice.com/documents/excel/2758-excel-split-date-and-time.html#a1
To separate the date and time.

I used a nested IF statement to determine AM or PM shift as follows:
=IF((AND(C7>=AM_start,C7<=AM_end)),"AM (SHIFT)",(IF((AND(C7>=PM_start,C7<=PM_end)),"PM (SHIFT)","NOT a Shift")))

(see Figure 1 below)

(see Figure 2 below)


Hope this helps

Figure 1. Layout

Figure 2. Formulas


2018-03-03 05:40:34

Anita

Hi all. Could someone help me.

Below are my data.

I need the shift to be determined automatically.

03:45 HRS - 14:15 HRS = AM (SHIFT)
14:16 HRS - 23:30 HRS = PM (SHIFT)

Time In SHIFT
2/15/2018 4:04 AM
2/15/2018 4:05 AM
2/15/2018 4:05 AM
2/15/2018 4:05 AM
2/15/2018 14:22 PM
2/15/2018 14:24 PM
2/15/2018 14:31 PM
2/15/2018 14:31 PM
2/15/2018 14:32 PM
2/15/2018 14:32 PM
2/15/2018 14:34 PM
2/15/2018 14:34 PM

I've tried many formulas but did not get the result. Pls pls help


2017-10-30 06:52:41

sai Kiran

There is a request in my project that need to know how many test steps are executed in 24 hours time ( I.e. Austin TX time zone)

Count of steps should be calculated between Yesterday 4:00 PM PST till today 3:59 PM PST

I will get raw data in excel like 'execution date' in one column and 'execution time' in next column .

Please help me on this and do let me know if you need further details.


2017-09-17 22:08:25

bob

I have tried the formulas listed above and only the sumproduct formula will count multiple cells containing the exact same time.


2017-09-07 19:08:28

Harish

I need the total number of cells in sheet1 Column A that are less than 16 hours in sheet 2 F7 cell.

For Ex
Sheet 1 Column A
2017/08/10 03:00:00
2017/07/01 00:00:00
2017/09/08 00:00:00
2017/09/08 02:00:00

Now Date & Time is 2017/09/08 14:00:00

Sheet 2 F7 cell should show 3

Now Date & Time is 2017/09/08 18:01:00

Sheet2 F7 cell should show 4.

Sheet 2 F7 cell should automatically update.


2017-07-20 10:41:48

Peter

Saura,

I wasn't prompted to upload my image so.

B5:D5 contained the times 8:00, 12:00 and 18:00

The formulas return 1 if they meet the criterion so we can use a simple sum in the range B3:D3


2017-07-20 10:34:33

Peter Atherton

Saura

I used the following formulas
Cell Formulas
B1 =TODAY()
D1 =B1-90
B6 =IF(LEN(A6)=0,"",IF(AND(A6>$D$1,$A6-INT($A6)>=B$5,$A6-INT($A6)<C$5),1,""))
C6 =IF(LEN(A6)>0,IF(AND($A6>$D$1,$A6-INT($A6)>=C$5,$A6-INT($A6)<D$5),1,""),"")
D6 =IF(LEN(A6)=0,"",IF($A6<$D$1,"",IF(OR($A6-INT($A6)>=D$5,$A6-INT($A6)<($B$5)),1,"")))
B3 =SUM(B$6:B$101)

Hope this helps
{{Fig}}



2017-07-19 10:19:34

Saura Tripathy

HI,

Very nice article. I stumbled upon this page while looking for a solution for time calculation. I have been tasked with data to find out how many tickets we received during AM, PM and Night hours for last 90 days. I searched a lot of articles now. But not been able to find any solution so far. It would be great in case you could help. (see Figure 1 below)

The date and time (Eg. 7/17/2017 10:40:54 PM) are mentioned in a single column with random dates for last 90 days. If I convert the times to proper format, I'll loose the date value. Attached a screenshot to explain the requirement. Appreciate any help or suggestion.

Regards,
SJ

Figure 1. 


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.