+ Reply to Thread
Results 1 to 5 of 5

Help with datediff vba

  1. #1
    John
    Guest

    Help with datediff vba


    Cells(Row,16) is a date formatted 02/13/04

    Worksheets(4).Cells(10, 5) = DateDiff("d", Date, Cells(Row,16) Doesn't work.

    I tried
    dim billed as date
    billed = cells(row,16)
    datediff("d",date, billed)
    but that doesn't work either.

    I need the difference in days between two dates. One is cells(row, 16)
    and the other is today's date. I imagine it has something to do with how
    the dates are formatted but I can't find anything about it.

    Thanks

    John

  2. #2
    Ron Rosenfeld
    Guest

    Re: Help with datediff vba

    On Tue, 15 Feb 2005 21:25:16 -0600, John <JOhn6328@mchsi.com> wrote:

    >
    >Cells(Row,16) is a date formatted 02/13/04
    >
    >Worksheets(4).Cells(10, 5) = DateDiff("d", Date, Cells(Row,16) Doesn't work.
    >
    >I tried
    >dim billed as date
    >billed = cells(row,16)
    >datediff("d",date, billed)
    >but that doesn't work either.
    >
    >I need the difference in days between two dates. One is cells(row, 16)
    >and the other is today's date. I imagine it has something to do with how
    >the dates are formatted but I can't find anything about it.
    >
    >Thanks
    >
    >John


    You'll need to post more information.

    What does "doesn't work" mean? Machine crashes? Excel crashes? Some error
    message? Wrong answer?

    The following works fine on my machine with some date in P1:

    ==============
    Sub foo()
    Dim billed As Date
    Const row As Integer = 1
    billed = Cells(row, 16)

    Debug.Print (DateDiff("d", Date, billed))
    End Sub
    ============

    Of course, if all you are looking for is the difference in days, then the
    simpler formula:

    SomeVariable = Date - billed

    would give the same answer.




    --ron

  3. #3
    Tom Ogilvy
    Guest

    Re: Help with datediff vba

    just subtract the earlier date from the later date. The integer part of the
    answer is the days between. The decimal part is the portion of a 24 hour
    day. Dates/Time are stored as the number of days from a base date (1900).
    So if you subtract them from each other, you get the difference in days.

    --
    Regards,
    Tom Ogilvy

    "John" <JOhn6328@mchsi.com> wrote in message
    news:%23eAvgc9EFHA.624@TK2MSFTNGP09.phx.gbl...
    >
    > Cells(Row,16) is a date formatted 02/13/04
    >
    > Worksheets(4).Cells(10, 5) = DateDiff("d", Date, Cells(Row,16) Doesn't

    work.
    >
    > I tried
    > dim billed as date
    > billed = cells(row,16)
    > datediff("d",date, billed)
    > but that doesn't work either.
    >
    > I need the difference in days between two dates. One is cells(row, 16)
    > and the other is today's date. I imagine it has something to do with how
    > the dates are formatted but I can't find anything about it.
    >
    > Thanks
    >
    > John




  4. #4
    Steve
    Guest

    Re: Help with datediff vba

    John

    1) Have you assigned a value to the variable 'Row' in your code? (I'd stear
    clear of using 'Row' as a variable name).


    The following works for me:
    Sub test()

    YourColumn = 1
    For Yourrow = 1 To 15
    Worksheets(1).Cells(Yourrow, YourColumn + 1) = DateDiff("d", Date,
    Cells(Yourrow, YourColumn))
    Next
    End Sub


    HTH
    Steve


    "John" <JOhn6328@mchsi.com> wrote in message
    news:%23eAvgc9EFHA.624@TK2MSFTNGP09.phx.gbl...
    >
    > Cells(Row,16) is a date formatted 02/13/04
    >
    > Worksheets(4).Cells(10, 5) = DateDiff("d", Date, Cells(Row,16) Doesn't
    > work.
    >
    > I tried
    > dim billed as date
    > billed = cells(row,16)
    > datediff("d",date, billed)
    > but that doesn't work either.
    >
    > I need the difference in days between two dates. One is cells(row, 16) and
    > the other is today's date. I imagine it has something to do with how the
    > dates are formatted but I can't find anything about it.
    >
    > Thanks
    >
    > John




  5. #5
    John
    Guest

    Re: Help with datediff vba

    Tom Ogilvy wrote:
    > just subtract the earlier date from the later date. The integer part of the
    > answer is the days between. The decimal part is the portion of a 24 hour
    > day. Dates/Time are stored as the number of days from a base date (1900).
    > So if you subtract them from each other, you get the difference in days.
    >

    Thanks, it wasn't working because I was referencing the wrong cell which
    was a string not a date.

    JOhn

+ 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