非常感谢您的回答 D_Bester。我试图在 VBA for Excel 中执行此操作。
我将您的 VB 代码翻译成 Excel 的 VBA。我想我会在这里发布我的代码,以供其他可能尝试该语言的人使用。
Sub displayHolidays()
Dim myHolidayList As New Collection
Set myHolidayList = GetHolidayList(2005)
For x = 1 To (myHolidayList.Count)
Debug.Print myHolidayList(x)
Next
End Sub
Function GetHolidayList(ByVal vYear As Integer) As Collection
Const FirstWeek As Integer = 1
Const SecondWeek As Integer = 2
Const ThirdWeek As Integer = 3
Const FourthWeek As Integer = 4
Const LastWeek As Integer = 5
Const DayofWeek_Monday As Integer = 2
Const DayofWeek_Thursday As Integer = 5
Const DayofWeek_Saturday As Integer = 7
Const DayofWeek_Sunday As Integer = 1
Dim holidayList As New Collection
Dim finalHolidayList As New Collection
' http://www.usa.gov/citizens/holidays.shtml
' http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp
' New Year's Day Jan 1
holidayList.Add (DateSerial(vYear, 1, 1))
' Martin Luther King, Jr. third Mon in Jan
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayofWeek_Monday, ThirdWeek))
' Washington's Birthday third Mon in Feb
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayofWeek_Monday, ThirdWeek))
' Memorial Day last Mon in May
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayofWeek_Monday, LastWeek))
' Independence Day July 4
holidayList.Add (DateSerial(vYear, 7, 4))
' Labor Day first Mon in Sept
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayofWeek_Monday, FirstWeek))
' Columbus Day second Mon in Oct
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayofWeek_Monday, SecondWeek))
' Veterans Day Nov 11
holidayList.Add (DateSerial(vYear, 11, 11))
' Thanksgiving Day fourth Thur in Nov
holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayofWeek_Thursday, FourthWeek))
' Christmas Day Dec 25
holidayList.Add (DateSerial(vYear, 12, 25))
'saturday holidays are moved to Fri; Sun to Mon
For Each dt In holidayList
If Weekday(dt) = DayofWeek_Saturday Then
dt = DateAdd("d", -1, dt)
End If
If Weekday(dt) = DayofWeek_Sunday Then
dt = DateAdd("d", 1, dt)
End If
finalHolidayList.Add dt
Next dt
'return
Set GetHolidayList = finalHolidayList
End Function
Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
'specify which day of which week of a month and this function will get the date
'this function uses the month and year of the date provided
Dim dtFirst As Date
Dim innerDate As Date
Dim monthAdd As Date
'get first day of the given date
dtFirst = DateSerial(Year(dt), Month(dt), 1)
'get first DayOfWeek of the month
innerDate = DateAdd("d", -DayofWeek, dtFirst)
GetNthDayOfNthWeek = DateAdd("d", (7 - Weekday(innerDate)), dtFirst)
'get which week
GetNthDayOfNthWeek = DateAdd("d", ((WhichWeek - 1) * 7), GetNthDayOfNthWeek)
'if day is past end of month then adjust backwards a week
monthAdd = DateAdd("m", 1, dtFirst)
If GetNthDayOfNthWeek >= monthAdd Then
GetNthDayOfNthWeek = DateAdd("d", (-7), GetNthDayOfNthWeek)
End If
End Function