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 !!!
 
with a little help of a friend : Translator
Excel Formula:
=IF(BY6="","",IF(AND(DATEDIF(BY6,NOW(),"y")<>0,
DATEDIF(BY6,NOW(),"ym")<>0),
DATEDIF(BY6,NOW(),"y")&" jaar en "&DATEDIF(BY6,NOW(),"ym")&" maand ","")
&IF(AND(DATEDIF(BY6,NOW(),"y")<>0,
DATEDIF(BY6,NOW(),"ym")=0),
DATEDIF(BY6,NOW(),"y")&" jaar ","")
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
hello all :)

i'm still trying to replace the many characters-datediff (dutch: datumverschil) into a udf called dv :)

i learned that the string deciding on days months or years should come first.
but still it gives an error.
because i use a "dutch excel" you should think you need to change all , into ; but i learned in a udf (in my case that is placed into a formula)
you have to form the vba in the english way...

so why is it still not working?

this is what it should do (just one example)

each row is a person; day of birth in column Q ; in column F it says "person x was born on tuesday, 15 march 1960 at 14:55"
in column E it says "person x is 60 years and 3 months old. his birthday is tomorrow".
for that second line, the part of the formula to say that is :
VBA Code:
[B]&ALS(AM6="";
ALS(365-DATUMVERSCHIL(Q6;VANDAAG();"yd")=2;"verjaart overmorgen"&TEKEN(10);"")
&ALS(365-DATUMVERSCHIL(Q6;VANDAAG();"yd")=1;"verjaart morgen"&TEKEN(10);"")
&ALS(EN(MAAND(Q6)=MAAND(VANDAAG());DAG(Q6)=DAG(VANDAAG()));"◀ verjaart vandaag ! ▶"&TEKEN(10);"");"")[/B]

as you can see the function "datumverschil" (english: datediff) is in these three lines already two times.
it calculates the years between birth (column Q) and today (dutch: vandaag)...
the part " datumverschil(q6;vandaag();"yd") should be in a udf saying dv(q6;vandaag();"yd")

so i have:
VBA Code:
[B]Function dv(eerste As Range, tweede As Range, str As String)
dv = DateDiff(str, eerste, tweede)[/B]
End Function

str is a string containing "yd"; eerste (english: first) is day of birth ; tweede (english: second) is date of today.

what should i change to get my "dv" udf in my formula :) ?

have a nice sunday :) !
 
Upvote 0
Do you remember this thread you posted a while back?


Now: read this reply you gave to someone trying to help you, and aks yourself that first question again…
let me recommend to you google translate it will tell you : nu = now.
i also told you i'm using my udf in formula, you know what that is?
i also told you that the reason i'm making udf is because my formula is at its limits (8192 characters)
so you also know what i presented as code is only a very small part of it.
and because i 'm sure you know how a datediff function works,

i'm asking myself why you are asking ten million questions, while you perfectly well
just could tell me how to write an udf to replace the datediff function.

thank you so far for only taking my time and making me angry.
i will not ask you anything else. just read again all the decent answers i already gave.
 
Upvote 0
Do you remember this thread you posted a while back?


Now: read this reply you gave to someone trying to help you, and aks yourself that first question again…
no need to hunt me down with that post. you prove i 'm right. i'm a very very patient person... and thank you for helping?
 
Upvote 0
you're not very polite
Cell Formulas
RangeFormula
A2:A39A2=RANDBETWEEN(DATE(1910,1,1),TODAY())
B2:B39B2=MROUND(YEAR(TODAY())*12+MONTH(TODAY())-12*YEAR(A2)-MONTH(A2),12)
C2:C39C2=+EDATE(A2,B2)
D2:D39D2=-TODAY()+C2
E2:E39E2=IF(ABS(D2)<30,CHOOSE(SIGN(D2)+2,"Verjaarde "&-D2 & " dagen terug","happy birthday","verjaart binnen "&D2&" dagen"),"")
G2:G39G2=DATEDIF(A2,TODAY(),"m")
 
Upvote 0
ok, for all those who think the same way as you, i really would like to apologise sincerely. in every possible language, so "sorry excuse-moi, entschuldige mich, perdone, ..."
but: watching your reply i must ask: where do i see a UDF doing what i 'm looking for ?

your line G2 " datedif(a2, today(), "m") would be in dutch: " datumverschil("m"; a2; vandaag())

so i still wonder how this udf with three variables (date, date, string) has to be composed?

sofar and still, i have this:

VBA Code:
Public Function dv(eerste As Date, tweede As Date, str As String) As Variant
' ==================================================== datumverschil
dv = DateDiff(str, eerste, tweede)
End Function

thank you for any help and have a great sunday !
 
Upvote 0
ssss.xlsm
ABCDEF
1number of months to nearest birthdayclosest birthdatedeltacommentUDF
210/05/195086410/05/202251 71 years, 10 months, 10 days
33/08/19686483/08/2022136 53 years, 7 months, 17 days
430/11/1918123630/11/2021-110 103 years, 3 months, 20 days
57/12/19785167/12/2021-103 43 years, 3 months, 13 days
619/01/201212019/01/2022-60 10 years, 2 months, 1 days
721/07/197161221/07/2022123 50 years, 7 months, 27 days
814/08/196568414/08/2022147 56 years, 7 months, 6 days
915/08/1929111615/08/2022148 92 years, 7 months, 5 days
109/04/19459249/04/202220verjaart binnen 20 dagen76 years, 11 months, 11 days
1130/07/196864830/07/2022132 53 years, 7 months, 20 days
1220/06/194394820/06/202292 78 years, 9 months, 0 days
1314/12/195678014/12/2021-96 65 years, 3 months, 6 days
1414/08/198643214/08/2022147 35 years, 7 months, 6 days
1515/10/194887615/10/2021-156 73 years, 5 months, 5 days
165/02/20081685/02/2022-43 14 years, 1 months, 15 days
1710/01/200619210/01/2022-69 16 years, 2 months, 10 days
1814/05/195185214/05/202255 70 years, 10 months, 6 days
199/11/19498649/11/2021-131 72 years, 4 months, 11 days
208/11/19716008/11/2021-132 50 years, 4 months, 12 days
2117/03/196469617/03/2022-3Verjaarde 3 dagen terug58 years, 0 months, 3 days|Verjaardag 3 dagen terug
2219/05/20149619/05/202260 7 years, 10 months, 1 days
2328/03/201113228/03/20228verjaart binnen 8 dagen10 years, 11 months, 20 days|Verjaardag binnen 8 dagen
246/11/19814806/11/2021-134 40 years, 4 months, 14 days
254/06/192212004/06/202276 99 years, 9 months, 16 days
2616/03/1931109216/03/2022-4Verjaarde 4 dagen terug91 years, 0 months, 4 days|Verjaardag 4 dagen terug
2715/02/201113215/02/2022-33 11 years, 1 months, 5 days
282/06/20121202/06/202274 9 years, 9 months, 18 days
297/09/19548167/09/2022171 67 years, 6 months, 13 days
3017/07/1911133217/07/2022119 110 years, 8 months, 3 days
319/07/19706249/07/2022111 51 years, 8 months, 11 days
326/01/19874206/01/2022-73 35 years, 2 months, 14 days
3313/07/1918124813/07/2022115 103 years, 8 months, 7 days
3412/05/195876812/05/202253 63 years, 10 months, 8 days
351/06/192012241/06/202273 101 years, 9 months, 19 days
3618/08/1914129618/08/2022151 107 years, 7 months, 2 days
3730/09/193999630/09/2022194 82 years, 5 months, 20 days
382/09/19587682/09/2022166 63 years, 6 months, 18 days
3916/01/1936103216/01/2022-63 86 years, 2 months, 4 days
Blad1
Cell Formulas
RangeFormula
A2:A39A2=RANDBETWEEN(DATE(1910,1,1),TODAY())
B2:B39B2=MROUND(YEAR(TODAY())*12+MONTH(TODAY())-12*YEAR(A2)-MONTH(A2),12)
C2:C39C2=+EDATE(A2,B2)
D2:D39D2=-TODAY()+C2
E2:E39E2=IF(ABS(D2)<30,CHOOSE(SIGN(D2)+2,"Verjaarde "&-D2 & " dagen terug","happy birthday","verjaart binnen "&D2&" dagen"),"")
F2:F39F2=leeftijd(A2)
 
Upvote 0
hello...

can you please check my udf to see what's wrong with it? thanks ...
 
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
 
Upvote 0
hello !

thank you so much for this udf... i do understand every part of it, but sadly it does not yet work... it still gives a error on value...

i put " =dv("m";q6;am6) " in a cell q6 day of birth; am6 day of death...
i have placed msgbox between all lines but not one msgbox shows up...

my suspicion is it has to do with me being in belgium, flemish, and using therefor the dutch excel version,
although i learned in vba you still use the english terms... i did try replacing , with ; but no result...

to be clear: the cells in eerste and tweede (first and second) are all valid dates, with the cells having a date number format being "dddd.mm.jjjj" ...

the search continues ;) !!! (thank you so far !!!)
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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