+ Reply to Thread
Results 1 to 5 of 5

Month Calculation

  1. #1
    Registered User
    Join Date
    12-07-2007
    Location
    Philadelphia, PA
    Posts
    18

    Month Calculation

    Hello,

    I am trying to create a formula that will allow me to calculate the # of months between two dates. The catch being that the two dates are contained in a single cell and they are displayed as mm/yy, rather than mm/dd/yy (i.e. 7/07-9/07, which should = 3 months). Also, months that are not in the fiscal year (7/1/2007-6/30/2008) should not be included in the count (i.e. 4/08-3/09 should = 3 months). Furthermore, any date ranges that are not within the 7/07-6/08 range should = 0 months (i.e. 7/05-6/06).

    This is the formula I have come up with, however, I cannot add any more levels, which forces me to ignore a few bugs that are inherent.

    =YEARFRAC(IF(VALUE("7/1/2007")>=VALUE(SUBSTITUTE(IF(ISBLANK(K5),"xxxxx",LEFT(K5,FIND("-",K5)-1)),"/","/01/20",1)),VALUE("7/1/2007"),VALUE(SUBSTITUTE(IF(ISBLANK(K5),"xxxxx",LEFT(K5,FIND("-",K5)-1)),"/","/01/",1))),IF(VALUE("6/30/2008")<=VALUE(SUBSTITUTE(SUBSTITUTE(IF(ISBLANK(K5),"xxxxx",RIGHT(K5,FIND("-",K5)-1)),"/","/28/",1),"-",,1)),VALUE("6/30/2008"),VALUE(SUBSTITUTE(SUBSTITUTE(IF(ISBLANK(K5),"xxxxx",RIGHT(K5,FIND("-",K5)-1)),"/","/28/",1),"-",,1))))*12

    K5=the cell with date range.

    Can anyone help me come up with a code that works everytime and is a bit more simple?

    It would be greatly appreciated!

    Thanks in advance.

    (P.S. sorry for the title misspelling >.<)

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Month Calucation

    Here's what I came up with....

    Please Login or Register  to view this content.
    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    12-07-2007
    Location
    Philadelphia, PA
    Posts
    18
    I made some minor changes just with the >/</= signs, the dates, and the FALSE return value, but other then that, it works great - thanks!

    Here's the formula with the minor amendments:

    =IF(AND(--SUBSTITUTE((LEFT(K5,FIND("-",K5)-1)),"/","/1/")<=--"6/30/08",--SUBSTITUTE(MID(K5,FIND("-",K5)+1,255),"/","/28/")>--"7/1/07"),DATEDIF(MAX(--"7/1/07",--SUBSTITUTE((LEFT(K5,FIND("-",K5)-1)),"/","/1/")),MIN(--"6/30/08",--SUBSTITUTE(MID(K5,FIND("-",K5)+1,255),"/","/28/")),"M")+1,0)

    Also, is there anyway to remove singular spaces like with the TRIM() function? The formula returns errors for a small portion of the cells because there's a space in between the date and the hyphen. Not a big problem, but just curious.

    Anyways, thanks again!

  4. #4
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Month Calucation

    Yes....TRIM is the correct function to remove the extra spaces...

    Please Login or Register  to view this content.
    Yikes! That formula is ugly as a mud fence!

  5. #5
    Registered User
    Join Date
    12-07-2007
    Location
    Philadelphia, PA
    Posts
    18
    Perfection

    Thank you.

+ 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