وعليكم السلام أخي الكريم حسين
ضع الدالة المعرفة التالية في موديول عادي
Function CalculateAge(birth As Variant, start As Variant, str As String)
Dim y As Long
Dim m As Long
Dim d As Long
If Not IsDate(birth) Or Not IsDate(start) Then GoTo Skipper
m = DateDiff("m", birth, start)
d = DateDiff("d", DateAdd("m", m, birth), start)
If d < 0 Then
m = m - 1
d = DateDiff("d", DateAdd("m", m, birth), start)
End If
y = m \ 12
m = m Mod 12
Select Case str
Case "d"
CalculateAge = d
Case "m"
CalculateAge = m
Case "y"
CalculateAge = y
End Select
Exit Function
Skipper:
CalculateAge = ""
End Function
ثم في حدث الفورم ضع الكود التالي
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsDate(TextBox1) Then
TextBox3 = CalculateAge(TextBox2, TextBox1, "y")
TextBox4 = CalculateAge(TextBox2, TextBox1, "m")
TextBox5 = CalculateAge(TextBox2, TextBox1, "d")
End If
End Sub
Private Sub UserForm_Activate()
Me.TextBox1.Value = Format("1/10/2017", "dd/mm/yyyy")
Me.TextBox2.Value = Format(Date, "dd/mm/yyyy")
End Sub