udf to calculate date difference value error

littlepete

Well-known Member
Joined
Mar 26, 2015
Messages
503
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:
     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
to give you an idea, a printscreen of my fake data showing you all i have :) you will see in column F that your udf is in it :) !!!
i am now trying to avoid with that leeftijd udf to have " he is 35 years, 3 months and 0 days " (avoiding zeros)

thank you !!!
 

Attachments

  • 2022-04-09.png
    2022-04-09.png
    152.5 KB · Views: 7
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
to give you an idea, a printscreen of my fake data showing you all i have :) you will see in column F that your udf is in it :) !!!
i am now trying to avoid with that leeftijd udf to have " he is 35 years, 3 months and 0 days " (avoiding zeros)

thank you !!!
Not a solution to your question, but an alternative to your "Project". www.ancestris.org
 
Upvote 0
it's like a spill formula, so you need 3 cells horizontal or with transpose 3 cells vertical (green cells)
You can also ask 1 item with the index-formula (orange cells).
if day of death is not a date or less than birthday then he/she is still alive
The day of death is optional, so if he/she is still alive, you're not obliged to add. (3rd example)
VBA Code:
Sub test()
     x = Leeftijd(DateSerial(1933, 1, 12), DateSerial(1998, 4, 2))
End Sub



Function Leeftijd(geboorte, Optional sterfte)                   'geboorte=day of birth
     Dim result(2), Dead                                        'array met 3 elementen (0-2)

     Dead = 0
     On Error Resume Next
     If IsDate(sterfte) Then If sterfte >= geboorte Then Dead = sterfte
     On Error GoTo 0

     '1e berekening = datumverschil tot vandaag
     maanden = DateDiff("m", geboorte, Date) + (Day(geboorte) > Day(Date))
     d = CInt(Date - WorksheetFunction.EDate(geboorte, maanden))
     result(0) = Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"

     If Dead > Leeftijd Then                                    'is overleden (want datum na geboorte)
          result(2) = "He/she would be " & result(0): result(0) = "Deceased"     'is die overleden, dan zet je dat datumverschil in result(2) en "deceased" in result(0)
     Else
          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
          End If

          result(2) = "Still alive": result(0) = result(0) & IIf(Len(s) > 0, " ", "") & s
     End If

     If Dead >= geboorte Then
          maanden = DateDiff("m", geboorte, Dead) + (Day(geboorte) > Day(Dead))
          d = CInt(Dead - WorksheetFunction.EDate(geboorte, maanden))
          result(1) = "Deceased at age of  " & Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"     'echte leeftijd
     Else
          result(1) = "Alive and kicking"
     End If

     Leeftijd = result
End Function
l_pete.xlsm
ABCDEF
1
212/01/19334/04/1998DeceasedDeceased at age of 65 years, 2 months, 23 daysHe/she would be 89 years, 3 months, 3 days
3
4
5DeceasedDeceased
6Deceased at age of 65 years, 2 months, 23 days
7Deceased at age of 65 years, 2 months, 23 daysHe/she would be 89 years, 3 months, 3 days
8
9He/she would be 89 years, 3 months, 3 days
10
11
12
13
14
15
161/01/20001/01/199922 years, 3 months, 14 daysAlive and kickingStill alive
17
18
1922 years, 3 months, 14 days22 years, 3 months, 14 days
20Alive and kicking
21Alive and kickingStill alive
22
23Still alive
24
25
26
27
28
29
30
31
3215/04/1922100 years, 0 months, 0 days Happy birthdayAlive and kickingStill alive
33
34
35100 years, 0 months, 0 days Happy birthday100 years, 0 months, 0 days Happy birthday
36Alive and kicking
37Alive and kickingStill alive
38
39Still alive
40
Blad1
Cell Formulas
RangeFormula
D2:F2,D16:F16D2=leeftijd(A2,B2)
D5D5=INDEX(leeftijd($A$2,$B$2),1)
F5:F7F5=TRANSPOSE(leeftijd($A$2,$B$2))
D7D7=INDEX(leeftijd($A$2,$B$2),2)
D9D9=INDEX(leeftijd($A$2,$B$2),3)
D19D19=INDEX(leeftijd($A$16,$B$16),1)
F19:F21F19=TRANSPOSE(leeftijd($A$16,$B$16))
D21D21=INDEX(leeftijd($A$16,$B$16),2)
D23D23=INDEX(leeftijd($A$16,$B$16),3)
A32A32=EDATE(TODAY(),-1200)
D32:F32D32=leeftijd(A32)
D35D35=INDEX(leeftijd($A$32),1)
F35:F37F35=TRANSPOSE(leeftijd($A$32))
D37D37=INDEX(leeftijd($A$16),2)
D39D39=INDEX(leeftijd($A$16),3)
Dynamic array formulas.
 
Upvote 0
it's like a spill formula, so you need 3 cells horizontal or with transpose 3 cells vertical (green cells)
You can also ask 1 item with the index-formula (orange cells).
if day of death is not a date or less than birthday then he/she is still alive
The day of death is optional, so if he/she is still alive, you're not obliged to add. (3rd example)
VBA Code:
Sub test()
     x = Leeftijd(DateSerial(1933, 1, 12), DateSerial(1998, 4, 2))
End Sub



Function Leeftijd(geboorte, Optional sterfte)                   'geboorte=day of birth
     Dim result(2), Dead                                        'array met 3 elementen (0-2)

     Dead = 0
     On Error Resume Next
     If IsDate(sterfte) Then If sterfte >= geboorte Then Dead = sterfte
     On Error GoTo 0

     '1e berekening = datumverschil tot vandaag
     maanden = DateDiff("m", geboorte, Date) + (Day(geboorte) > Day(Date))
     d = CInt(Date - WorksheetFunction.EDate(geboorte, maanden))
     result(0) = Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"

     If Dead > Leeftijd Then                                    'is overleden (want datum na geboorte)
          result(2) = "He/she would be " & result(0): result(0) = "Deceased"     'is die overleden, dan zet je dat datumverschil in result(2) en "deceased" in result(0)
     Else
          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
          End If

          result(2) = "Still alive": result(0) = result(0) & IIf(Len(s) > 0, " ", "") & s
     End If

     If Dead >= geboorte Then
          maanden = DateDiff("m", geboorte, Dead) + (Day(geboorte) > Day(Dead))
          d = CInt(Dead - WorksheetFunction.EDate(geboorte, maanden))
          result(1) = "Deceased at age of  " & Int(maanden / 12) & " years, " & maanden Mod 12 & " months, " & d & " days"     'echte leeftijd
     Else
          result(1) = "Alive and kicking"
     End If

     Leeftijd = result
End Function
l_pete.xlsm
ABCDEF
1
212/01/19334/04/1998DeceasedDeceased at age of 65 years, 2 months, 23 daysHe/she would be 89 years, 3 months, 3 days
3
4
5DeceasedDeceased
6Deceased at age of 65 years, 2 months, 23 days
7Deceased at age of 65 years, 2 months, 23 daysHe/she would be 89 years, 3 months, 3 days
8
9He/she would be 89 years, 3 months, 3 days
10
11
12
13
14
15
161/01/20001/01/199922 years, 3 months, 14 daysAlive and kickingStill alive
17
18
1922 years, 3 months, 14 days22 years, 3 months, 14 days
20Alive and kicking
21Alive and kickingStill alive
22
23Still alive
24
25
26
27
28
29
30
31
3215/04/1922100 years, 0 months, 0 days Happy birthdayAlive and kickingStill alive
33
34
35100 years, 0 months, 0 days Happy birthday100 years, 0 months, 0 days Happy birthday
36Alive and kicking
37Alive and kickingStill alive
38
39Still alive
40
Blad1
Cell Formulas
RangeFormula
D2:F2,D16:F16D2=leeftijd(A2,B2)
D5D5=INDEX(leeftijd($A$2,$B$2),1)
F5:F7F5=TRANSPOSE(leeftijd($A$2,$B$2))
D7D7=INDEX(leeftijd($A$2,$B$2),2)
D9D9=INDEX(leeftijd($A$2,$B$2),3)
D19D19=INDEX(leeftijd($A$16,$B$16),1)
F19:F21F19=TRANSPOSE(leeftijd($A$16,$B$16))
D21D21=INDEX(leeftijd($A$16,$B$16),2)
D23D23=INDEX(leeftijd($A$16,$B$16),3)
A32A32=EDATE(TODAY(),-1200)
D32:F32D32=leeftijd(A32)
D35D35=INDEX(leeftijd($A$32),1)
F35:F37F35=TRANSPOSE(leeftijd($A$32))
D37D37=INDEX(leeftijd($A$16),2)
D39D39=INDEX(leeftijd($A$16),3)
Dynamic array formulas.
hello :)

thank you for your reply !!!
i'm not sure how to use this udf: i'm trying " leeftijd(q6) " (q is the column with birth date)
or " leeftijd(q6;am6) " (q is birth and am is date of death)

i always get an "overloop" error meaning there is a result but the udf cannot present it. i think
in english it's called "spill error"...

what should i change?
enjoy excel, and have a great Eastern !
 
Upvote 0
that UDF calculates 3 things, if you just want one of them use the index
Rich (BB code):
=index(leeftijd(q6;am6);2) 
=index(leeftijd(q6);1) 
 
Upvote 0

Forum statistics

Threads
1,216,122
Messages
6,128,958
Members
449,480
Latest member
yesitisasport

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