从不同的文本框中减去 2 个日期并在第三个文本框中自动填充年龄
Subtract 2 dates from different textbox and auto populate age in 3rd textbox
How to populate age automatically after i enter Date of birh
我想从不同的文本框中减去 2 个日期并在第 3 个文本框中自动填充年龄
Textbox7 的日期是今天
Textbox3 有出生日期
在 textbox4 中,我只想在(年)内自动填充 AGE。
感谢您的帮助。
提前致谢!
雪利酒
为每个有效条目格式使用 If/ElseIf 代码块并适当添加世纪。
Private Sub TextBox3_AfterUpdate()
Dim s As String, dob As Date
Dim age As Long, bday As Date
s = TextBox3.Text 'dob
TextBox4 = "" ' age
If Len(s) = 0 Then
Exit Sub
End If
If IsNumeric(s) Then
If s Like "##" Then
dob = DateSerial("19" & s, 1, 1)
ElseIf TextBox3 Like "0##" Then
dob = DateSerial("2" & s, 1, 1)
ElseIf TextBox3 Like "####" Then
dob = DateSerial(s, 1, 1)
Else
TextBox3 = ""
Exit Sub
End If
ElseIf s Like "*#/*#/*##" And IsDate(s) Then
dob = DateValue(s)
Else
TextBox3 = ""
Exit Sub
End If
' calc age if valid dob
If IsDate(dob) Then
If dob > Date Then
MsgBox "Birthday in future ?"
Else
TextBox3 = Format(dob, "dd/mm/yyyy")
' had birthday this year ?
bday = DateSerial(Year(Date), Month(dob), Day(dob))
age = Year(Date) - Year(dob)
If Date < bday Then age = age - 1
TextBox4 = age
End If
Else
TextBox3 = ""
End If
End Sub
How to populate age automatically after i enter Date of birh
我想从不同的文本框中减去 2 个日期并在第 3 个文本框中自动填充年龄
Textbox7 的日期是今天
Textbox3 有出生日期
在 textbox4 中,我只想在(年)内自动填充 AGE。
感谢您的帮助。
提前致谢!
雪利酒
为每个有效条目格式使用 If/ElseIf 代码块并适当添加世纪。
Private Sub TextBox3_AfterUpdate()
Dim s As String, dob As Date
Dim age As Long, bday As Date
s = TextBox3.Text 'dob
TextBox4 = "" ' age
If Len(s) = 0 Then
Exit Sub
End If
If IsNumeric(s) Then
If s Like "##" Then
dob = DateSerial("19" & s, 1, 1)
ElseIf TextBox3 Like "0##" Then
dob = DateSerial("2" & s, 1, 1)
ElseIf TextBox3 Like "####" Then
dob = DateSerial(s, 1, 1)
Else
TextBox3 = ""
Exit Sub
End If
ElseIf s Like "*#/*#/*##" And IsDate(s) Then
dob = DateValue(s)
Else
TextBox3 = ""
Exit Sub
End If
' calc age if valid dob
If IsDate(dob) Then
If dob > Date Then
MsgBox "Birthday in future ?"
Else
TextBox3 = Format(dob, "dd/mm/yyyy")
' had birthday this year ?
bday = DateSerial(Year(Date), Month(dob), Day(dob))
age = Year(Date) - Year(dob)
If Date < bday Then age = age - 1
TextBox4 = age
End If
Else
TextBox3 = ""
End If
End Sub