How do I calculate a person's age? You can use the DateAdd and DateDiff functions to calculate the time that has elapsed between two dates. With a little additional work, you can present that time in the desired format. For example, the following procedure calculates a person's age in years, taking into account whether his or her birthday has already occurred in the current year.
Using the DateDiff function to determine the number of years between today and a birthdate doesn't always give a valid result, because the DateDiff function rounds to the next year. If a person's birthday hasn't yet occurred, using the DateDiff function will make the person one year older than he or she actually is.
To remedy this situation, the procedure checks to see whether the birthday has already occurred this year; if it hasn't, the procedure subtracts 1 to return the correct age.
Function CalcAge(dteBirthdate As Date) As Long
Dim lngAge As Long
' Make sure passed-in value is a date. If Not IsDate(dteBirthdate) Then dteBirthdate = Date End If
' Make sure birthdate is not in the future. ' If it is, use today's date. If dteBirthdate > Date Then dteBirthdate = Date End If
' Calculate the difference in years between today and birthdate. lngAge = DateDiff("yyyy", dteBirthdate, Date) ' If birthdate has not occurred this year, subtract 1 from age. If DateSerial(Year(Date), Month(dteBirthdate), Day(dteBirthdate)) > Date Then lngAge = lngAge - 1 End If CalcAge = lngAge End Function
Svaret fra puppetmaster, har jeg svært ved at konvertere til brug med en form, en tekstboks og en label. Så om muligt, ville jeg meget gerne have det omformuleret.
Svaret fra kedde65 fungere ikke helt. Når jeg indtaster min søns fødselsdag 14-08-97 får jeg svaret 5 år. Det passer jo ikke helt, idet han lige nu er 4 år - fylder først 5 til august. Hvordan kan den lille forskel omgås?
Den lille fejl jeg fik lagt ind i mit :-)) Kan rettes ved at gøre det som pupertmaster. Send mig din word fil, så skal jeg klarede det for dig. Du kender jo mail adressen :-))
VH CK
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.