+ Reply to Thread
Results 1 to 24 of 24

Check if time range is within another time range

  1. #1
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Question Check if time range is within another time range

    Hello dear people!

    I would like to ask your help with the following problem.

    I want to check if a time range as example below is within another time range, this also needs to pass midnight as you can see.

    Example 1:

    A B C Result
    15:00-16:00 08:00-16:00 1
    16:00-17:00 08:00-16:00 0
    17:00-18:00 08:00-16:00 0

    Example 2:

    A B C Result
    15:00-16:00 17:00-01:00 0
    16:00-17:00 17:00-01:00 0
    17:00-18:00 17:00-01:00 1

    I appreciate your help!

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    Welcome to the forum.

    There are instructions at the top of the page explaining how to attach your sample workbook.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Thank you,

    I missed that Attached the example file
    Attached Files Attached Files

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Check if time range is within another time range

    A
    B
    C
    D
    1
    Result should be
    formula
    2
    15:00-16:00 08:00-16:00
    1
    1
    3
    16:00-17:00 08:00-16:00
    0
    0
    4
    17:00-18:00 08:00-16:00
    0
    0
    5
    6
    Result should be
    7
    15:00-16:00 17:00-01:00
    0
    0
    8
    16:00-17:00 17:00-01:00
    0
    0
    9
    17:00-18:00 17:00-01:00
    1
    1


    A
    B
    C
    D
    2
    15:00-16:00 08:00-16:00
    1
    =LET(
    startTime1,FILTERXML("<x><y>"&SUBSTITUTE(A2,"-","</y><y>")&"</y></x>","//y[1]"),
    endTime1,FILTERXML("<x><y>"&SUBSTITUTE(A2,"-","</y><y>")&"</y></x>","//y[2]"),
    startTime2,FILTERXML("<x><y>"&SUBSTITUTE(B2,"-","</y><y>")&"</y></x>","//y[1]"),
    endTime2,FILTERXML("<x><y>"&SUBSTITUTE(B2,"-","</y><y>")&"</y></x>","//y[2]"),
    IF(endTime2>startTime2,IF(AND(startTime2<=startTime1,endTime2>=endTime1),1,0),IF(AND(startTime2<=startTime1,endTime2+"24:00:00">=endTime1),1,0)))
    3
    16:00-17:00 17:00-01:00
    0
    =LET(
    startTime1,FILTERXML("<x><y>"&SUBSTITUTE(A3,"-","</y><y>")&"</y></x>","//y[1]"),
    endTime1,FILTERXML("<x><y>"&SUBSTITUTE(A3,"-","</y><y>")&"</y></x>","//y[2]"),
    startTime2,FILTERXML("<x><y>"&SUBSTITUTE(B3,"-","</y><y>")&"</y></x>","//y[1]"),
    endTime2,FILTERXML("<x><y>"&SUBSTITUTE(B3,"-","</y><y>")&"</y></x>","//y[2]"),
    IF(endTime2>startTime2,IF(AND(startTime2<=startTime1,endTime2>=endTime1),1,0),IF(AND(startTime2<=startTime1,endTime2+"24:00:00">=endTime1),1,0)))
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Check if time range is within another time range

    In D2

    =(1*LEFT(B2,5)<1*RIGHT(A2,5))*(1*RIGHT(B2,5)>1*LEFT(A2,5))

    In D7

    =1*(1*LEFT(B7,5)<1*RIGHT(A7,5))
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  6. #6
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi Kokosek,

    Thank you so much for your reply!

    I have taken the if statement and instead used LEFT RIGHT for the start and end time. I had to add IFERROR as well because when a cell is empty it throw me a #VALUE. This works like a charm.

    There is only 1 issue I'm facing and that's that my formula is very long and I would need to check multiple rows with the same formula in 1 cell so I do a + and the same formula again changing the row numbers.

    Example is attached.

    This is the formula:

    =IF(ISNUMBER(SEARCH("*Test*";$A$2));IF(IFERROR(VALUE(RIGHT(C$2;5))>VALUE(LEFT(C$2;5)););IF(IFERROR(AND(VALUE(LEFT(C$2;5))<=VALUE(LEFT($B13;5));VALUE(RIGHT(C$2;5))>=VALUE(RIGHT($B13;5))););1;0);IF(IFERROR(AND(VALUE(LEFT(C$2;5))<=VALUE(LEFT($B13;5));VALUE(RIGHT(C$2;5))+"24:00:00">=VALUE(RIGHT($B13;5))););1;0)))

    And when I want to check the new row I add + and the same formula and change the row number on column A and C

    +IF(ISNUMBER(SEARCH("*Test*";$A$3));IF(IFERROR(VALUE(RIGHT(C$3;5))>VALUE(LEFT(C$3;5)););IF(IFERROR(AND(VALUE(LEFT(C$3;5))<=VALUE(LEFT($B13;5));VALUE(RIGHT(C$3;5))>=VALUE(RIGHT($B13;5))););1;0);IF(IFERROR(AND(VALUE(LEFT(C$3;5))<=VALUE(LEFT($B13;5));VALUE(RIGHT(C$3;5))+"24:00:00">=VALUE(RIGHT($B13;5))););1;0)))

    Is there any way to shorten this formula or how to repeat it without having to write the whole formula with a +
    Attached Files Attached Files

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    Did you see post #5?

  8. #8
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi AliGW,

    Yes checked post #5 as well. And thanks a lot for taking the time to check my question kvsrinivasamurthy! highly appreciated.

    I don't think will work sadly, I tried I need to check a range of hours. Can refer to the attachment I put under #6

    Thanks again!

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    Well, that certainly shifts the goalposts! Completely different to the way you presented your problem at the outset!!!

  10. #10
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Those were 2 examples I gave to cover normal hours and after midnight hours. So I could start to work with something. If I need a new thread for this question I posted in #6 I will and mark this as resolved. As they did resolve the issue I gave

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    Be careful - we do not allow duplicate threads on the same issue, so only do that if you are asking about something entirely different.

  12. #12
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Okay. then I'll leave it like this and see KOKOSEK or kvsrinivasamurthy would still want to have a look at my post #6

    Fingers crossed :D I've been struggling with this for a week

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    They will - just be patient.

  14. #14
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Check if time range is within another time range

    I am not able to follow what you want. Pl give full data in its original format and give the expected result with suitable explanation. Upload new sample file. In the first post you compared data in the same row. That is what the result says. That is A2 is compared to B2, A3 to B3 etc. Do you want to compare B2 with full range A2:A4 . B3 also with A2:A4. Same applies to second table also. Pl clarify.

  15. #15
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi kvsrinivasamurthy,

    My example is in post #6

    So what I basically am looking for is to see which hours are covered. For example IF in A2 the word "Test" appears I want it to check the corresponding cell, in this case C3. If C3 is covering the hour 08:00-09:00 in B13 then it should show 1.

    As you see in A3 there is no "Test" written but "Different" so it doesn't count it.

    Problem is (at least I don't know how) that the formula will be too long if I repeat the formula 30+ times in 1 cell. it will go over the character limit.

    I took IF(endTime2>startTime2,IF(AND(startTime2<=startTime1,endTime2>=endTime1),1,0),IF(AND(startTime2<=startTime1,endTime2+"24:00:00">=endTime1),1,0)) from KOKOSEK in Post: #4

    I hope it's a bit more clear

  16. #16
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Check if time range is within another time range

    C3 is compared with B13:B28 result in C13:C28
    For C4:C10 show the result should be in column D to Column I?
    Pl see file
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi, kvsrinivasamurthy

    No sorry, I gave you a better example now. Just imagine now there are 40 more rows that the same needs to be checked for.

    Then the formula will be too long to put it in 1 cell.
    Attached Files Attached Files

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,399

    Re: Check if time range is within another time range

    This is still problematic. You need to fill in the table at the bottom MANUALLY to show us the outcomes you WANT. Sharing a non-working formula gets us no further forward.

    Try again, please.

  19. #19
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi see attached.

    I hope it's clear. The formula works. But I need to shorten it or find a different way to combine the same formula in 1 cell and not reach the limit of 9000~ characters
    Attached Files Attached Files

  20. #20
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Check if time range is within another time range

    The following array entered formula**, pasted into cell C13 and then copied down and over, yields the same results as those shown in the file attached to post #19:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    **Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
    Note that the commas may need to be changed to semicolons based on regional settings.
    Note that if there are 40 more rows then change the 10's in the formula into 50's i.e. =SUM(IFERROR((ISNUMBER(SEARCH("*Test*",$A$2:$A$50...
    Let us know if you have any questions.
    Last edited by JeteMc; 09-22-2021 at 12:13 PM. Reason: Added file
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  21. #21
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Hi JeteMc

    This is amazing!, I didn't think about an array myself. I tried and it works only after midnight hours don't work

    For example, in your file you attached if in C10 you put 16:00-24:00 it works perfectly. But when you put for example 18:00-02:00 or anything else after midnight it doesn't work.

    I would have to check the formula itself I think, if you or someone else sees the solution to that I would highly appreciate it

    Thanks so much!

  22. #22
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Check if time range is within another time range

    Try the following**:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    **Array entered formula (see post #20)
    Let us know if you have any questions.

  23. #23
    Registered User
    Join Date
    09-20-2021
    Location
    Netherlands
    MS-Off Ver
    O365
    Posts
    11

    Re: Check if time range is within another time range

    Thanks works!

  24. #24
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,869

    Re: Check if time range is within another time range

    You're Welcome. Thank You for marking the thread as 'Solved'. I hope that you have a blessed day.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 07-23-2019, 11:09 AM
  2. Replies: 2
    Last Post: 06-26-2018, 09:10 PM
  3. [SOLVED] Check if range of time falls in another range of time
    By wafs in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-18-2018, 06:47 PM
  4. Check if event occurs within time range
    By excellinginlife in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 09-20-2016, 10:43 AM
  5. [SOLVED] Counting values in a time range - when the time range crosses midnight
    By dlocos in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-24-2014, 04:27 AM
  6. Replies: 4
    Last Post: 08-04-2011, 03:54 PM
  7. Check value in a time range
    By badin in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-25-2008, 06:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1