Consulting

Results 1 to 7 of 7

Thread: Is ElectionDay Function

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location

    Is ElectionDay Function

    Does anyone have a function to determine if a given date falls on Election Day of the given year?


    For example, if I passed 11/5/2018 to the function it would return False. If I passed 1/6/2018 it would return true.

    Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,607
    Location
    Presumably, that'd be something like:
    Function IsElectionDay(Dt As Date) As Boolean
    If (Format(Dt, "m/d") = "1/6") And (Format(Dt, "yyyy") Mod 4 = 2) Then
      IsElectionDay = True
    Else
      IsElectionDay = False
    End If
    End Function
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location
    Paul,

    My understanding is that the U.S. Election Day is the first Tuesday (after the first Monday) in November. Clunky perhaps, but the following seems to work:


    Sub TE()
      MsgBox fcnIsElectionDay("11/6/2018")
      MsgBox fcnIsElectionDay("11/6/2019") 'False
      MsgBox fcnIsElectionDay("11/5/2019")
      MsgBox fcnIsElectionDay("11/8/2022")
    End Sub
    Public Function fcnIsElectionDay(oDate As Date) As Boolean
    Dim lngMonth As Integer, lngWD As Integer
      fcnIsElectionDay = False
      lngWD = Weekday(oDate)
      lngMonth = Month(oDate)
      If lngMonth = 11 And lngWD = 3 Then
        If Month(DateAdd("d", -1, oDate)) = 11 Then
          If Day(oDate) < 9 And Day(oDate) > 1 Then
            fcnIsElectionDay = True
          End If
         End If
      End If
    lbl_Exit:
      Exit Function
    End Function
    Thank for looking at it though.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,607
    Location
    So how, in your first post, would 1/6/2018 return true? FWIW, not knowing the US political system in detail, I assumed that date was the post-election inauguration day and that's what you meant.

    Last edited by macropod; 10-09-2018 at 03:12 PM.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location
    Paul,

    Don't you just love it when people can't pay enough attention to get the details right? That was typo. I meant 11/6/2018. Yes, I've kicked myself in my own head for it.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,607
    Location
    Don't you need to test the year, too? AFAIK, the presidential election is only every 4th year and there are mid-term elections 2 years after that but none is held in the 1st or 3rd years. Correct? If so, try:
    Function IsElectionDay(Dt As Date) As Boolean
    IsElectionDay = False
    If Format(Dt, "yyyy") Mod 2 = 0 Then
      If Format(Dt, "mm") = 11 Then
        If Dt Mod 7 = 3 Then
          If (Format(Dt, "d") > 1) And (Format(Dt, "d") < 9) Then
            IsElectionDay = True
          End If
        End If
      End If
    End If
    End Function
    Change Mod 2 to Mod 4 if you're only interested in the Presidential elections.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location
    Paul,

    Yes, for Federal Elections that is true. For my purposes though, I was using the Pennsylvania statute which declares the First Tuesday (after the first Monday) in November (regardless of year) as Election day. I suppose they must have state or local elections every year.

    Thanks. Yes, your function works for what you defined and works well.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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