+ Reply to Thread
Results 1 to 6 of 6

Thread: DateDiff gives incorrect year difference value

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

    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.

    YearsofService = DateDiff("yyyy", c.Value, Date)
    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 Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    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?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

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

    Re: DateDiff gives incorrect year difference value

    37870 formatted left

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

    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.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

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

    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?

    Private Sub Workbook_Open()
    Dim YearsofService As Integer, lngLastRow As Long, rngDates As Range
    
    lngLastRow = Sheets("Basic Info").Range("R65536").End(xlUp).Row
    Set rngDates = Sheets("Basic Info").Range("R2:R" & lngLastRow)
    For Each c In rngDates
        If c.Value > 0 Then
            YearsofService = DateDiff("yyyy", c.Value, Date)
            If YearsofService < 5 Then
                c.Offset(0, 7) = "1 month"
            End If
            If YearsofService = 5 Then
                c.Offset(0, 7) = "5 weeks"
            End If
            If YearsofService = 6 Then
                c.Offset(0, 7) = "6 weeks"
            End If
            If YearsofService = 6 Then
                c.Offset(0, 7) = "6 weeks"
            End If
            If YearsofService = 7 Then
                c.Offset(0, 7) = "7 weeks"
            End If
            If YearsofService = 8 Then
                c.Offset(0, 7) = "8 weeks"
            End If
            If YearsofService = 9 Then
                c.Offset(0, 7) = "9 weeks"
            End If
            If YearsofService = 10 Then
                c.Offset(0, 7) = "10 weeks"
            End If
            If YearsofService = 11 Then
                c.Offset(0, 7) = "11 weeks"
            End If
            If YearsofService = 12 Then
                c.Offset(0, 7) = "12 weeks"
            End If
            If YearsofService > 12 Then
                c.Offset(0, 7) = "12 weeks"
            End If
    
        End If
    Next c
    End Sub
    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.

    Sub UpdateonOpen()
       Dim c.value As Integer, lngLastRow As Long, rngDates As Range
    
    lngLastRow = Sheets("Basic Info").Range("R65536").End(xlUp).Row
    Set rngDates = Sheets("Basic Info").Range("X2:X" & lngLastRow)
    For Each c In rngDates
        If c.Value > 0 Then
            If c.Value < 5 Then
                c.Offset(0, 2) = "1 month"
            End If
            If c.Value = 5 Then
                c.Offset(0, 2) = "5 weeks"
            End If
            If c.Value = 6 Then
                c.Offset(0, 2) = "6 weeks"
            End If
            If c.Value = 6 Then
                c.Offset(0, 2) = "6 weeks"
            End If
            If c.Value = 7 Then
                c.Offset(0, 2) = "7 weeks"
            End If
            If c.Value = 8 Then
                c.Offset(0, 2) = "8 weeks"
            End If
            If c.Value = 9 Then
                c.Offset(0, 2) = "9 weeks"
            End If
            If c.Value = 10 Then
                c.Offset(0, 2) = "10 weeks"
            End If
            If c.Value = 11 Then
                c.Offset(0, 2) = "11 weeks"
            End If
            If c.Value = 12 Then
                c.Offset(0, 2) = "12 weeks"
            End If
            If c.Value > 12 Then
                c.Offset(0, 2) = "12 weeks"
            End If
    
        End If
    Next c
    
    End Sub
    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
    11

    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.2.0