udf to calculate date difference value error

littlepete

Well-known Member
Joined
Mar 26, 2015
Messages
507
Office Version
  1. 365
Platform
  1. Windows
hello :)

an udf to compose all parts of a name (title, surname, first names, last name, transcription if other script) that is working
made me very happy !
i'm trying now to use (datumverschil in dutch: ) datedifference. it has three parts: first date; second date; way of measuring...

this is why i'm trying... probably i'm forgetting something?
i try to use it this way : " dv(q6;am6;"d") "

VBA Code:
Function dv(eerste As Range, tweede As Range, str As String)
dv = DateDiff(eerste, tweede, str)
End Function

thank you for the correction or help !!!
 
VBA Code:
' ===================== datumverschil (date difference)
Public Function dv(eerste As Range, tweede As Range, Interval As String) As Variant

    Dim DiffDate As Long

    Interval = LCase(Trim(Interval))
    Select Case Interval
    Case "yyyy"                                       '= Year
        DiffDate = DateDiff("yyyy", eerste, tweede)
    Case "q"                                          '= Quarter
        DiffDate = DateDiff("q", eerste, tweede)
    Case "m"                                          '= Month
        DiffDate = DateDiff("m", eerste, tweede)
    Case "y"                                          '= Day of year
        DiffDate = DateDiff("y", eerste, tweede)
    Case "d"                                          '= Day
        DiffDate = DateDiff("d", eerste, tweede)
    Case "w"                                          '= Weekday
        DiffDate = DateDiff("w", eerste, tweede)
    Case "ww"                                         ' = Week
        DiffDate = DateDiff("ww", eerste, tweede)
    Case "h"                                          '= Hour
        DiffDate = DateDiff("h", eerste, tweede)
    Case "n"                                          '= Minute
        DiffDate = DateDiff("n", eerste, tweede)
    Case "s"                                          '= Second
        DiffDate = DateDiff("s", eerste, tweede)
    Case Else
        DiffDate = "Fout!"
    End Select

    dv = DiffDate
End Function
hello ;) i'm not sure if i posted my answer on the correct place so here a reminder in case it was not ;) thx !!!
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
i put " =dv("m";q6;am6) " in a cell q6 day of birth; am6 day of death...
Why would you ever do that? Didn't you look at the code that I posted?

VBA Code:
Public Function dv(eerste As Range, tweede As Range, Interval As String) As Variant

You must enter the parameters in the the order required by the function as I wrote it if you expect it to work for you.

tmp1.xlsm
QRSAM
1eerste tweedeIntervalResult
61-Mar10-Juld131
71-Mar10-Julm4
Sheet5
Cell Formulas
RangeFormula
AM6:AM7AM6=dv(Q6,R6,S6)
 
Upvote 0
Why would you ever do that? Didn't you look at the code that I posted?

VBA Code:
Public Function dv(eerste As Range, tweede As Range, Interval As String) As Variant

You must enter the parameters in the the order required by the function as I wrote it if you expect it to work for you.

tmp1.xlsm
QRSAM
1eerste tweedeIntervalResult
61-Mar10-Juld131
71-Mar10-Julm4
Sheet5
Cell Formulas
RangeFormula
AM6:AM7AM6=dv(Q6,R6,S6)

hello ;)

i was only referring to post number 20 here; i DID copy your code entirely, and then wrote post number 20 here ;) but i forgot to answer you and just made a new post,
therefor this reminder i had answered already... so: up to number 20 :)
 
Upvote 0
hello ;)

i was only referring to post number 20 here; i DID copy your code entirely, and then wrote post number 20 here ;) but i forgot to answer you and just made a new post,
therefor this reminder i had answered already... so: up to number 20 :)
I don't understand the above. The question I have for you is why did you attempt to use =dv("m";q6;am6) when the code I posted (and the code you posted in post #1) expect to see =dv(q6;am6;"m")?

[Note that the issue of separation characters (i.e. ";" vs. "," ) is yours to resolve. My version of excel is set up to use commas (",") so for me on my PC the formula is =dv(q6,am6,"m")]
 
Upvote 0
I don't understand the above. The question I have for you is why did you attempt to use =dv("m";q6;am6) when the code I posted (and the code you posted in post #1) expect to see =dv(q6;am6;"m")?

[Note that the issue of separation characters (i.e. ";" vs. "," ) is yours to resolve. My version of excel is set up to use commas (",") so for me on my PC the formula is =dv(q6,am6,"m")]
hey...

in the beginning i did not know the difference dutch (end with string) and english (start with string) ... in my dutch formula all works perfect...
trying to put it in an udf is difficult, i am trying all differences... thxxx for the help :) !
 
Upvote 0
sunday, so i had to take a family break ...
My version is also dutch/flemish.
i can add "application.volatile", but that's your choice.
VBA Code:
Function Leeftijd(geboorte)                                     'geboorte=day of birth
     maanden = DateDiff("m", geboorte, Date) + (Day(geboorte) > Day(Date))
     d = CInt(Date - WorksheetFunction.EDate(geboorte, maanden))
     Leeftijd = Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"     'echte leeftijd

     Delta = CInt(WorksheetFunction.EDate(geboorte, WorksheetFunction.MRound(maanden - (Day(geboorte) > Day(Date)), 12)) - Date)     'even afronden op een jaar
     If Abs(Delta) < 20 Then
          Select Case Sgn(Delta)
               Case -1: s = "Verjaardag " & -Delta & " dagen terug"
               Case 0: s = "Happy birthday"
               Case 1: s = "Verjaardag binnen " & Delta & " dagen"
          End Select
          Leeftijd = Leeftijd & "|" & s
     End If
End Function
 
Upvote 0
VBA Code:
' ===================== datumverschil (date difference)
Public Function dv(eerste As Range, tweede As Range, Interval As String) As Variant

    Dim DiffDate As Long

    Interval = LCase(Trim(Interval))
    Select Case Interval
    Case "yyyy"                                       '= Year
        DiffDate = DateDiff("yyyy", eerste, tweede)
    Case "q"                                          '= Quarter
        DiffDate = DateDiff("q", eerste, tweede)
    Case "m"                                          '= Month
        DiffDate = DateDiff("m", eerste, tweede)
    Case "y"                                          '= Day of year
        DiffDate = DateDiff("y", eerste, tweede)
    Case "d"                                          '= Day
        DiffDate = DateDiff("d", eerste, tweede)
    Case "w"                                          '= Weekday
        DiffDate = DateDiff("w", eerste, tweede)
    Case "ww"                                         ' = Week
        DiffDate = DateDiff("ww", eerste, tweede)
    Case "h"                                          '= Hour
        DiffDate = DateDiff("h", eerste, tweede)
    Case "n"                                          '= Minute
        DiffDate = DateDiff("n", eerste, tweede)
    Case "s"                                          '= Second
        DiffDate = DateDiff("s", eerste, tweede)
    Case Else
        DiffDate = "Fout!"
    End Select

    dv = DiffDate
End Function

it took some time but this is the solution, let me explain:

the INSIDE of the udf has to be completely english, so using kommas and not ";" also: the interval comes first.
the OUTSIDE is the opposit: i use a dutch excel, so formula are in dutch vba ! so:
using ";" and not kommas ; and the interval goes last !!!

that combination makes the udf with datediff / datumverschil working !!!

thank you for the help ! I am very glad to have found the solution !!!
 
Upvote 0
sunday, so i had to take a family break ...
My version is also dutch/flemish.
i can add "application.volatile", but that's your choice.
VBA Code:
Function Leeftijd(geboorte)                                     'geboorte=day of birth
     maanden = DateDiff("m", geboorte, Date) + (Day(geboorte) > Day(Date))
     d = CInt(Date - WorksheetFunction.EDate(geboorte, maanden))
     Leeftijd = Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"     'echte leeftijd

     Delta = CInt(WorksheetFunction.EDate(geboorte, WorksheetFunction.MRound(maanden - (Day(geboorte) > Day(Date)), 12)) - Date)     'even afronden op een jaar
     If Abs(Delta) < 20 Then
          Select Case Sgn(Delta)
               Case -1: s = "Verjaardag " & -Delta & " dagen terug"
               Case 0: s = "Happy birthday"
               Case 1: s = "Verjaardag binnen " & Delta & " dagen"
          End Select
          Leeftijd = Leeftijd & "|" & s
     End If
End Function
hello ;)

thank you for this perfectly working function!
I would like to ask one more thing to adjust:
now the case -1 0 1 shows birthdays in the past / today / future.
could you adjust it so that it also says:
birthday two days ago ; birthday yesterday ; birthday tomorrow ; birthday day after tomorrow
(birthday -2 -1 0 1 2 and 3-30 in future/past (which is there already)

that would make my text complete :) ! thank you for your effort :) !!!
 
Upvote 0
VBA Code:
     delta =...
     Select Case Delta
          Case 0: s = "Happy birthday"
          Case -1: s = "yesterday ..."
          Case -2: s = "before yesterday ..."
          Case 1: s = "tomorrow ..."
          Case 2 = "the day after tomorrow"
          Case -20 To -3: s = "Verjaardag " & -Delta & " dagen terug"
          Case 3 To 20: s = "Verjaardag binnen " & Delta & " dagen"
          Case Else: s = ""
     End Select
     if len(s)>0 then Leeftijd = Leeftijd & "|" & s
 
Upvote 0
Solution
VBA Code:
     delta =...
     Select Case Delta
          Case 0: s = "Happy birthday"
          Case -1: s = "yesterday ..."
          Case -2: s = "before yesterday ..."
          Case 1: s = "tomorrow ..."
          Case 2 = "the day after tomorrow"
          Case -20 To -3: s = "Verjaardag " & -Delta & " dagen terug"
          Case 3 To 20: s = "Verjaardag binnen " & Delta & " dagen"
          Case Else: s = ""
     End Select
     if len(s)>0 then Leeftijd = Leeftijd & "|" & s
thank you for the solution :) it works perfectly and it is soooooo much shorter then my formula hahaha :) ! have a great day !!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top