+ Reply to Thread
Results 1 to 8 of 8

number of months between two dates

  1. #1
    Forum Contributor
    Join Date
    04-08-2008
    Posts
    121

    number of months between two dates

    Hi,

    I have a long list of start dates and finish dates. See attached columns M-N. In column O I want to calculate the difference in "months" of the 2 dates. Can I do this using the VBA? I think I can use the datediff function. I want to click a button and the months to update.

    Thanks for your help
    Baz
    Attached Files Attached Files

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Why use a macro, this formula should do it

    =MONTH(N2)-MONTH(M2)
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    You don't need VBA to do this ... just use the formula: "=MONTH(N2)-MONTH(M2)" in cell O2 and copy down (change number format to an integer).
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436
    The datediff code would be
    Please Login or Register  to view this content.
    The formula would be

    =(MONTH(N2)-MONTH(M2))+((YEAR(N2)-YEAR(M2))*12)
    Cheers
    Andy
    www.andypope.info

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    LOL ... yes, good catch Andy! Didn't take into account the cases where the dates run over the year end It's always the simple things that bite you in the butt huh?

  6. #6
    Forum Contributor
    Join Date
    04-08-2008
    Posts
    121
    Hi Andy,

    Thanks for this but when I run the code I get a mis match type error in the formula bit.

    Thanks
    Harry

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    Quote Originally Posted by Andy Pope View Post
    The formula would be

    =(MONTH(N2)-MONTH(M2))+((YEAR(N2)-YEAR(M2))*12)
    DATEDIF worksheet formula (with only 1 "F") can also give you same result, i.e.

    =DATEDIF(M2-DAY(M2)+1,N2,"m")

    although both that formula and the above would count 30th November to 1st December as 1 month. If you use just

    =DATEDIF(M2,N2,"m") that only counts "whole months"

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436
    Not sure what you mean by
    I get a mis match type error in the formula bit.

+ 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