+ Reply to Thread
Results 1 to 12 of 12

Checking if time is between 2-4 hours after another time

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Checking if time is between 2-4 hours after another time

    Hello,

    I would like to get some grip on the syntax of a formula in excel 2007.

    Logic is I want C2 to = "yes" if

    B2's time is between 2 and 4 hours after A2's time.

    Any help is much appreciated.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,712

    Re: Checking if time is between 2-4 hours after another time

    Assuming the times will be on the same day then try this

    =IF(AND((B2-A2)*24>=2,(B2-A2)*24<=4),"Yes","No")
    Audere est facere

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Hello,

    I keep getting a "missing then or go to" message:

    I'm doing it in VBA though, and the code is:

    Please Login or Register  to view this content.
    I'm getting syntax error.
    Attached Files Attached Files
    Last edited by Lifeseeker; 11-22-2011 at 06:22 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Can anybody help on this please?

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Minor correction:
    The logic is:

    If B2 is >=4, E2 will = "Yes" only if (C2 - A2) <=30 is true AND D2's time is between 2 and 4 hours after C2.

    Otherwise, E2 will say "No"

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Checking if time is between 2-4 hours after another time

    Hi Lifeseeker

    This is the entire code in your workbook
    Please Login or Register  to view this content.
    As you can see...there's a bit missing. There's an
    Please Login or Register  to view this content.
    missing and an
    Please Login or Register  to view this content.
    missing...not to mention...there's a reference to an "n" that's not defined. There's no way to trouble shoot this...not enough information...or code.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Hi,

    How about this version? I get no syntax error anymore, but it doesn't do the trick.

    Please Login or Register  to view this content.
    the logic is:

    If B2 is >=4, E2 will = "Yes" only if (C2 - A2) <=30 is true AND D2's time is between 2 and 4 hours after C2. (this is an example of a row-level calculation)

    "n" is time difference displayed in minutes?

    Thanks
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Can someone help with this please?

  9. #9
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Checking if time is between 2-4 hours after another time

    Can someone help?

  10. #10
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: Checking if time is between 2-4 hours after another time

    hello
    may be this approach
    =IF(HOUR(A1-B1)>HOUR(2),"yes","")

  11. #11
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: Checking if time is between 2-4 hours after another time

    try this also:
    =IF(OR(HOUR(A1-B1)>2,HOUR(A1-B1)<=4),"yes","No")

  12. #12
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Checking if time is between 2-4 hours after another time

    Lifeseeker,

    Attached is a modified version of your example workbook. Cell C2 didn't have an actual date because the "pm" was immediately next to the "12:30". VBA doesn't care, but formulas do. So here's VBA code for a Worksheet_Change event to perform as requested, and it will convert all dates entered incorrectly (like "7/4/11 12:30pm") to correct dates ("7/4/11 12:30:00 PM") and then check if E should be Yes or No in accordance to your logic stated above:
    Please Login or Register  to view this content.
    Last edited by tigeravatar; 11-28-2011 at 01:00 PM.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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