+ Reply to Thread
Results 1 to 6 of 6

If date is between two periods from a DatedIf function

  1. #1
    Forum Contributor
    Join Date
    11-07-2014
    Location
    UK
    MS-Off Ver
    2010
    Posts
    126

    If date is between two periods from a DatedIf function

    Hi all,

    I have been using a Dated if function to return how many years, months and days have occurred between two dates. I am now trying to use an if function to tell me if this result is between two time periods. The formula however, is not working all the time.

    So, I have grouped it into the following: -

    less than 4 months = if(result<"0 years, 4 months, 0 days")
    more than 4 months but less than 1 year = if(and(result >="0 years, 4 months, 0 days",result<"1 Years, 0 months, 0 days")
    more than 1 year but less than 2 years = as above
    more than 2 years but less than 5 years = as above
    more than 5 years but less than 10 years = as above
    more than 10 years = if(result>"10 years, 0 months, 0 days")

    The formula is working on anything for less than 4 months, 4 months to 1 year, 1-2 years and 10 years plus but, anything between 2 and 10 years is coming back as 10+ years.

    Any help would be appreciated, I am unable to upload an example file at the moment but, can do later on.

  2. #2
    Forum Expert etaf's Avatar
    Join Date
    10-22-2004
    Location
    Wittering, West Sussex, UK
    MS-Off Ver
    365 (Mac OSX) (16.84 (24041420))
    Posts
    8,751

    Re: If date is between two periods from a DatedIf function

    perhaps a sample file
    sounds like if all the other values are working, then perhaps a syntax error of some kind or if a nested IF , then maybe layout of the IF nest
    Wayne
    if my assistance has helped, and only if you wish to , there is an "* Add Reputation" on the left hand side - you can add to my reputation here

    If you have a solution to your thread - Please mark your thread solved do the following: >
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: If date is between two periods from a DatedIf function

    Try the attached file, maybe a solution.
    Enter start date in column "A" - end date in column "B" both in dd/mm/yyyy format.
    Attached Files Attached Files

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: If date is between two periods from a DatedIf function

    version 2, cleaning up maximums, minimums and negatives.
    Attached Files Attached Files

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,928

    Re: If date is between two periods from a DatedIf function

    Torachan - sorry for off-topic interjection:

    Although there is no official rule regarding this behaviour, we request that wherever possible both the question AND the answer be provided in substantive detail here within the thread. An attached workbook is an excellent aid for posing a question and offering a solution, but solely doing that with no in thread explanation makes it difficult for researchers to understand or consider the Q & A of this thread without downloading what may be a pointless doc to them, if they can do that at all. Doing that also hides the content from search engines so others may never benefit from this.
    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.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  6. #6
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,304

    Re: If date is between two periods from a DatedIf function

    AliGW - my apoligise.
    I believed the thread to be intact and the solution offered to be to simplistic to require explaining.
    However;
    Format columns "A" & "B" , Custom, dd/mm/yyyy
    Open VBA editor, (Alt + F11)
    In project window select sheet1
    copy and paste the code below into the code window and save and exit
    run the app and enter relevant dates in columns "A"(start date) & "B"(end date)
    On moving from cell in column "B" a message box will appear with relevant comment.
    The code can be further extended by replacing the message box with further code to manipulate any relevant data

    'code ------------------------------------------------------------------------------------
    Option Explicit
    Dim x As Double

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells1 As Range, KeyCells2 As Range
    Set KeyCells1 = Range("A:A")
    Set KeyCells2 = Range("B:B")
    If Not Application.Intersect(KeyCells2, Range(Target.Address)) Is Nothing Then
    x = DateDiff("d", Target.Cells(1, 0), Target.Cells(1, 1))
    Select Case x
    Case Is < 1
    MsgBox "DATE CONFLICT OR NEGATIVE RESULT"
    Case 1 To 121
    MsgBox "THE PERIOD IS LESS THAN 4 MONTHS"
    Case 122 To 365
    MsgBox "THE PERIOD IS GREATER THAN 4 MONTHS BUT LESS THAN 1 YEAR"
    Case 366 To 732
    MsgBox "THE PERIOD IS GREATER THAN 1 YEAR BUT LESS THAN 2 YEARS"
    Case 733 To 1826
    MsgBox "THE PERIOD IS GREATER THAN 2 YEARS BUT LESS THAN 5 YEARS"
    Case 1827 To 3652
    MsgBox "THE PERIOD IS GREATER THAN 5 YEARS BUT LESS THAN 10 YEARS"
    Case Is > 3652
    MsgBox "THE PERIOD IS GREATER THAN 10 YEARS"
    Case Else
    MsgBox "AMBIGUOS RESULT"
    End Select
    End If
    End Sub
    '---------------------------------------------------------------------------

+ 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. Automatically stop counting date difference in datedif function
    By Md Aftab Uzzaman in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-13-2018, 08:33 PM
  2. [SOLVED] Nested if with today function and datedif function
    By KIMMEYER in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-06-2016, 10:12 AM
  3. [SOLVED] DATEDIF - DATEDIF Calculation returning a negative for days or months
    By DaveBre in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-27-2015, 06:22 PM
  4. Use DATEDIF function in getting Age
    By IMA_Saihat in forum Tips and Tutorials
    Replies: 10
    Last Post: 04-10-2015, 01:34 PM
  5. Replies: 2
    Last Post: 05-21-2014, 02:26 PM
  6. Strange results from DATEDIF (Month to date) if the second date changed !
    By bebo021999 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-01-2013, 06:46 AM
  7. #NUM! while using DATEDIF function
    By sriradh in forum Excel General
    Replies: 3
    Last Post: 08-22-2008, 03:21 PM

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