+ Reply to Thread
Results 1 to 5 of 5

DateDiff

  1. #1
    Registered User
    Join Date
    10-31-2006
    Posts
    6

    DateDiff

    Hey guys, I'm trying to find out how I would compare the days in two date cells, and write this number (ie, 4) into a cell.

    I've been told that I might need to use the DateDiff function, but it's all looking a bit confusing!

    Cheers,
    Pete

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    DateDiff is only available via VBA code
    You will need to use one of these two examples

    As a macro
    Sub MyMacro()
    Range("a1").Value = DateDiff("d", Range("b1").Value, Range("c1").Value)
    End Sub

    or as an Excel function which is avaiable as a normal Excel function

    Function MyFunction(Date1 As Range, Date2 As Range) As Integer
    MyFunction = DateDiff("d", Date1, Date2)
    End Function

    Once the above function code is entered into a VBA module sheet the function is available in a spreadsheet
    to use in a1 type in =MyFunction(b1,c1)
    in b1 & c1 enter 2 different dates

    You can change the function name to any name that suits you just remember to change all entries from MyFunction to your desired name.

    The "d" in the datediff means dispaly the number of days different between the dates, the result can be seconds, minutes, hours, days etc by changing the d to one of these

    yyyy Year
    q Quarter
    m Month
    y Day of year
    d Day
    w Weekday
    ww Week
    h Hour
    n Minute
    s Second

  3. #3
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon

    There is a =DateDif() function available as a standard worksheet function. Unfortunately, it doesn't appear in the Insert > Function list, or in the help (although it was documented in the Help system of XL2000, but mysteriously never before or since – no-one’s quite sure why but legal reasons are often cited). Chip Pearson’s pages offer a nice tutorial on this mysterious function.

    http://www.cpearson.com/excel/datedif.htm

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    DominicB
    Thanks for the info. It makes it easier than my suggestion

  5. #5
    Registered User
    Join Date
    10-31-2006
    Posts
    6
    Thanks, both of 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