Explanation
If you need to group times into buckets that are not the same size (i.e. 12 AM-7 AM, 7 AM-12 PM, etc.) you can use the VLOOKUP function in approximate match mode.
The problem
There are several ways to group times in Excel. If you just need to group times by the hour, a pivot table is very quick and easy. If you need to group times into other equal buckets of multiple hours (i.e. 3 hours, 4 hours, etc.) a nice solution is to use the FLOOR function . However, if you need to group times into unequal buckets, you need to take a more custom approach. VLOOKUP, in its approximate match mode, allows you to group times into custom intervals of any size.
The solution
The solution is to build a lookup table that “maps” each time into the right bucket. In the first column, enter the start time for the bucket. In column two, enter the name of the bucket you want to use. The table must be sorted by the start time, smallest to largest. Finally, configure the VLOOKUP function to look up each time in the bucket table with approximate match.
In the example shown, the formula n E5 is:
=VLOOKUP(D5,buckets,2,1)
D5 is the lookup value, “buckets” is a named range for G5:H8, 2 is the column index, and 1 is a flag that enables approximate match. (You can also use TRUE). See this page for a full explanation.
When VLOOKUP is in approximate match mode, it matches the nearest value that is less than or equal to the lookup value . In this way, you can think of the incoming lookup time as being “rounded down” into the right bucket.
This formula is a great example of how you can use VLOOKUP to group data in completely custom ways. I learned it from Jon Acampora, over at Excel Campus, in his article on three ways to group times .
Explanation
This formula uses two named ranges : things , and results . If you are porting this formula directly, be sure to use named ranges with the same names (defined based on your data). If you don’t want to use named ranges, use absolute references instead.
The core of this formula is this snippet:
ISNUMBER(SEARCH(things,B5)
This is based on another formula ( explained in detail here ) that checks a cell for a single substring. If the cell contains the substring, the formula returns TRUE. If not, the formula returns FALSE.
Because we are giving the SEARCH function more than one thing to look for, in the named range things , it will give us more the one result, in an array that looks like this:
{#VALUE!;9;#VALUE!;#VALUE!}
Numbers represent matches in things , errors represent items that were not found.
To simplify the array, we use the ISNUMBER function to convert all items in the array to either TRUE or FALSE. Any valid number becomes TRUE, and any error (i.e. a thing not found) becomes FALSE. The result is an array like this:
{FALSE;TRUE;FALSE;FALSE}
which goes into the MATCH function as the lookup_array argument, with a lookup_value of TRUE:
MATCH(TRUE,{FALSE;TRUE;FALSE;FALSE},0) // returns 2
MATCH then returns the position of first TRUE found, 2 in this case.
Finally, we use the INDEX function to retrieve a result from the named range results at that same position:
=INDEX(results,2) // returns "found red"
You can customize the results range with whatever values make sense in your use case.
Preventing false matches
One problem with this approach with the ISNUMBER + SEARCH approach is you may get false matches from partial matches inside longer words. For example, if you try to match “dr” you may also find “Andrea”, “drank”, “drip”, etc. since “dr” appears inside these words. This happens because SEARCH automatically does a “contains-type” match.
For a quick fix, you can wrap search words in space characters (i.e. " dr “, or “dr “) to avoid finding “dr” in another word. But this will fail if “dr” appears first or last in a cell.
If you need a more robust solution, one option is to normalize the text first in a helper column , and add a leading and trailing space. Then use the formula on this page on the text in the helper column, instead of the original text.