Results 1 to 10 of 22

Thread: UDF (user defined function) replacement for Excel's DATEDIF function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    OK, that fixed it, but only the code you originally provided works properly. The second one gives me a value of 0 when I subtract today() from today() - 1, and the last one still gives me a #VAULE! error. I'd like to be able to use the last one because of its compactness, but any one that I can use is acceptable.

    I have a question about the code. What does Err.Raise 5 do? Also, is "Select Case" like a switch statement?

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by ProspectiveCounselor View Post
    OK, that fixed it, but only the code you originally provided works properly. The second one gives me a value of 0 when I subtract today() from today() - 1, and the last one still gives me a #VAULE! error. I'd like to be able to use the last one because of its compactness, but any one that I can use is acceptable.
    I think your problem is you did not inverse the arguments when you used TODAY()-1... if you look at the header for the function, namely...

    Function xlDATEDIF(ByVal StartDate As Date, ByVal EndDate As Date, Interval As String) As Variant

    you will see that the first argument is the StartDate (that is, the earlier date) and the second argument is the EndDate (the later date), since TODAY()-1 is earlier than TODAY(), it should be the first argument in the function... I have a feeling you did not do that. By the way, the only code I can speak to is the main, "longer" code in the first message... the other code was written by a contributor named 'snb'. I would point out that my code is longer because it provides for all of the original DATEDIF function that it is modeled after... in looking at the code provided by 'snb', I note he does not provide for the YM, YD or MD interval options. You should keep that in mind when deciding on using the "shorter" code over the "longer" code.

    Quote Originally Posted by ProspectiveCounselor View Post
    I have a question about the code. What does Err.Raise 5 do? Also, is "Select Case" like a switch statement?
    As the name implies, it raises an error, to be exact, it raises the "Invalid procedure call or argument" error (it is how the program communicates to the outside world that something is wrong with one or more of the arguments to the function when, in fact, something is actually wrong with the argument that has been passed into the function.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    I think your problem is you did not inverse the arguments when you used TODAY()-1... if you look at the header for the function, namely...
    Well, I really don't have to. I could just switch TODAY() - 1 to TODAY() + 1 to get an inverse because TODAY() - (TODAY() - 1) = (TODAY() + 1) - TODAY(). That's what I did, but I still got the #VALUE error.

    Quote Originally Posted by Rick Rothstein View Post
    As the name implies, it raises an error, to be exact, it raises the "Invalid procedure call or argument" error (it is how the program communicates to the outside world that something is wrong with one or more of the arguments to the function when, in fact, something is actually wrong with the argument that has been passed into the function.
    Is there a VBA Doc place like the JavaDocs on Oracle's website where I could go look up that object? I would like to know more.

Similar Threads

  1. Recommendation: Do not use the undocumented DATEDIF function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 4
    Last Post: 05-16-2015, 10:06 PM
  2. How To Use Frequency Function In Excel
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 1
    Last Post: 04-09-2013, 11:49 AM
  3. Creating drop-down function in excel
    By Jorrg1 in forum Excel Help
    Replies: 4
    Last Post: 01-09-2013, 01:45 PM
  4. Nested Search Function Excel Formula
    By trankim in forum Excel Help
    Replies: 6
    Last Post: 10-29-2012, 10:29 PM
  5. Excel Nested IF Function With 3 Conditions
    By patsir in forum Excel Help
    Replies: 3
    Last Post: 08-25-2012, 07:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •