• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

How to Find Fourth Thursday of November in VBA

June 18, 2014 by Ryan 2 Comments

I recently built a project where I had to count business days by excluding weekends and US Federal holidays. In my previous post: , my example used hard-coded dates. This new example can be adapted and re-coded to find the date of any specific weekday or holiday of the year without the need to hard-code.

Public Function dThanksgiving(curDate As Date) As Date
'Define variables
Dim curYear As Integer 'Current Year
Dim sDate As Date      'First Day of Month
Dim eDate As Date      'Last Day of Month
Dim ctDate As Date     'Count Date
Dim dCount As Integer  'Weekday hit counter

'Pull year value from input: curDate
curYear = Format(curDate, "yyyy")

'Set Start Date
sDate = CDate("11/01/" & curYear)
'Set End Date
eDate = CDate("11/30/" & curYear)
'Count Date starts at start date
ctDate = sDate

'Define Loop from 1 to the number of days
'difference between start and end date
For i = 1 To DateDiff("d", sDate, eDate)
    If Weekday(ctDate) = vbThursday Then
        'As weekdays are getting checked
        'add 1 to the counter when hitting
        'a thursday
        dCount = dCount + 1
        'If thursday counter equal 4, you
        'have found the fourth thursday of
        'the month of November
        If dCount = 4 Then
            'Return the date of the fourth
            'thursday
            dThanksgiving = ctDate
        End If
    End If
    ctDate = ctDate + 1
Next i

End Function

The above example can be passed any date and will return the fourth thursday of the month for the year of the date passed in.

Thats all I have for now. Please comment with questions.

Filed Under: Microsoft Access, VBA, VBScript Tagged With: access, Excel, Find Day of Week, VB

Reader Interactions

Comments

  1. Raghu Prabhu says

    February 22, 2018 at 11:52 pm

    I want to find out the first Thursday in November every year. How do I modify this please? Plus do you have a file. I don’t know how to use this function.

    Thanks.

    Reply
    • Ryan McCormick says

      March 3, 2018 at 3:50 pm

      Did you try swapping vbThursday for vbTuesday AND “If dCount = 4” to “If dCount = 1”?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  1. KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  2. jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  3. Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  4. mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  5. Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in