+ Reply to Thread
Results 1 to 6 of 6

DateDiff gives incorrect year difference value

  1. #1
    Registered User
    Join Date
    08-24-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    DateDiff gives incorrect year difference value

    Last week I was helped to get the solution to moving down a column, and to updating a another column as the result of the calculation below. YearsofService is defined as an integer in the VBA code.

    Please Login or Register  to view this content.
    When c.value = 06/09/2003 I would expect the years difference to be 7 until next Tuesday. It is coming up as 8. The date format for c.value is dd/mm/ccyy and the locale English(UK).

    What have I done wrong?

    Murray
    Last edited by murraymr; 08-30-2011 at 02:54 AM. Reason: Solved it !

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: DateDiff gives incorrect year difference value

    If you format c as General, what number appears?

    More simply, if you restore Excel's default alignment, does it align left or right?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-24-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: DateDiff gives incorrect year difference value

    37870 formatted left

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: DateDiff gives incorrect year difference value

    Ah. Unlike DATEDIF, from Help,

    When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.
    Similarly, DateDiff("m", "12/31/2003", "1/1/2004") returns 1.

  5. #5
    Registered User
    Join Date
    08-24-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: DateDiff gives incorrect year difference value

    Here is the code that was using DateDiff. I have tried to modify it to use DateDif, but it is not accepted. How can I execute a formula in a macro or VBA module?

    Please Login or Register  to view this content.
    I have tried adding a column to hold the formula and its result and updating the macro to use the resulting value, but the Dim line is found to be in error.

    Please Login or Register  to view this content.
    Help! I am a complete newbie to Excel programming

    Murray

  6. #6
    Registered User
    Join Date
    08-24-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: DateDiff gives incorrect year difference value

    I realised that, when I replaced YearsofService with 'c.value' this replacement updated the Dim line as well. When I removed 'c.value' from it, it worked!

    Thank you for the DateDif tip

    Murray

+ 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