I created a small userform with a textbox, a label and two buttons.
This is the code that I had in behind the form:
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim okToContinue As Boolean
Dim myAge As Long
Me.Label1.Caption = ""
okToContinue = True
If IsDate(Me.TextBox1.Value) Then
'keep looking
Else
okToContinue = False
End If
If okToContinue = False Then
Me.Label1.Caption = "Please check Date!"
Exit Sub
End If
myAge = Age(CDate(Me.TextBox1.Value), Date)
If myAge > 16 _
Or myAge < 14 Then
Me.Label1.Caption = "Not right age: " & myAge
Else
Me.Label1.Caption = "Woohoo"
'keep going to add the stuff to the worksheet
End If
End Sub
I wanted to return just the year from Chip's Age routine. This was the slightly
modifed code that was placed in a general module.
Option Explicit
Function Age(Date1 As Date, Date2 As Date) As Long 'String
Dim Y As Integer
Dim M As Integer
Dim D As Integer
Dim Temp1 As Date
Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
D = Day(Date2) - Day(Date1)
If D < 0 Then
M = M - 1
D = Day(DateSerial(Year(Date2), Month(Date2), 0)) + D
End If
Age = Y '& " years " & M & " months " & D & " days"
End Function
Ok, I found this code:
Function Age(Date1 As Date, Date2 As Date) As String
Dim Y As Integer
Dim M As Integer
Dim D As Integer
Dim Temp1 As Date
Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
D = Day(Date2) - Day(Date1)
If D < 0 Then
M = M - 1
D = Day(DateSerial(Year(date2), Month(date2), 0)) + D
End If
Age = Y & " years " & M & " months " & D & " days"
End Function
But it doesn't really say how/where to put that into the VBA, or how to make
the pop up box come up if the person is between 14 and 16.
Any suggestions?