Urgent help with VBA in Excel!
Hi guys,
I urgently need to create a function in Excel and I think I need to use VBA, but to be honest, I don't have a clue.
I need to make sure that when someone inputs their date of birth, they are over 18. If they are not over 18, a pop-up should appear to say: "The bartender must be 18 years of age."
I'm also struggling because I need to create the formula that uses today's (or tomorrow, the next day etc.) date as the date to work out the date of birth.
Any help would be massively appreciated.
Thanks,
Ryan
Re: Urgent help with VBA in Excel!
Welcome to VBForums :wave:
Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.
Re: Urgent help with VBA in Excel!
Try something like this:
Code:
Sub ageCheck()
Dim bDate As Date
Dim checkDate As Date
Dim diff As Double
Dim absGap As Double
bDate = InputBox("Enter your birthdate as 'mm/dd/yy'")
checkDate = Now 'could be "Now + 1" for tomorrow...
diff = (checkDate - bDate) / 365.25 'account for leap years, not 100% precise
absGap = Abs(18 - diff) 'how big is gap between NOW and birthdate (minus 18 yrs)
If absGap < 0.004 Then 'play with this number
If Month(bDate) <= Month(checkDate) Then
If Day(bDate) <= Day(checkDate) Then
MsgBox "Old enough"
Else
MsgBox "Not old enough"
End If
Else
MsgBox "Not old enough"
End If
Else
If diff >= 18 Then
MsgBox "Old enough to serve drinks"
Else
MsgBox "NOT old enough"
End If
End If
End Sub