Results 1 to 3 of 3

Thread: Urgent help with VBA in Excel!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    1

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Urgent help with VBA in Excel!

    Welcome to VBForums

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width