下午好,
我想帮助修改VBA代码,该代码将在考虑美国联邦假期的同时返回一个月的第n个营业日期。例如,如果我想返回2014年1月18日工作日的日期,则该值应为2014年1月28日。 1月份有两个联邦假日,其日期为28日,而不是24日。
我能找到的唯一代码是:
finaldate = Application.Evaluate("workday(Date(2014,01,0),18)")
这将是第18个工作日,但不考虑假期。
我确实已经建立了代码,将返回VBA内所有联邦假期的日期。这些值会根据当前年份自动更新。我宁愿不必引用工作簿上的任何表。
'Holiday Check
NewYearsDay = "01/01/" & EndYear ' New Year's Day, January 1st.
MLKJrDay = NDow(EndYear, 1, 3, 2) ' Birthday of Martin Luther King, third Monday in January.
PresidentsDay = NDow(EndYear, 2, 3, 2) ' Washington's Birthday, third Monday in February since 1971; prior to that year, it was celebrated on the traditional date of February 22.
MayMondayCount = DOWsInMonth(EndYear, 5, 2) ' Count number of Mondays in the month of May. Used to determine Memorial Day Date.
MemorialDay = NDow(EndYear, 5, MayMondayCount, 2) ' Memorial Day, last Monday in May since 1971; from 1868 to 1970 it was celebrated on May 30, and was called Decoration Day for part of that time.
IndepDay = "07/04/" & EndYear ' United States of America's Independence Day, July 4.
LaborDay = NDow(EndYear, 9, 1, 2) ' Labor Day, first Monday in September.
ColumbDay = NDow(EndYear, 10, 2, 2) ' Columbus Day, second Monday in October (federal holiday since 1971).
VeterDay = "11/11/" & EndYear ' Veterans Day, November 11th (except from 1971 to 1977, inclusive, when it was celebrated on the fourth Monday in October; formerly known as Armistice).
ThanksgDay = NDow(EndYear, 11, 4, 5) ' Thanksgiving Day, fourth Thursday in November.
ChristDay = "12/25/" & EndYear ' Christmas Day, December 25th.
任何有关如何在VBA中构建代码以实现此目的的帮助都将受到赞赏。
谢谢。
答案 0 :(得分:0)
首先,此答案应归功于 Simoco 。他上面的评论解决了我的问题。我想回答这个问题,这是我知道的唯一方法。
简短的回答是Workday是使用Excel 2007的方法。如果您使用的是Excel 2010,Workday
或Workday_Intl
也可以使用。两种选择都允许用户输入应排除的假期。我使用Dateserial
作为我的约会,并使用我之前计算的假日日期构建了一个数组。
Sub Test_toFind_Business_Day()
Dim StartMonth As Long, StartYear As Long, StartDay As Long, DayCount As Long
Dim ws As Worksheet
Dim finaldate As Date, newDate As Date, DateHolder As Date
Dim holidays As Variant
Dim EndYear As Integer
Set ws = ThisWorkbook.Sheets("Invoice_Criteria")
StartDate = ws.Range("PreviousSettlement").Value
EndDate = DateAdd("m", 1, StartDate)
EndYear = DatePart("yyyy", EndDate)
DayCount = ws.Range("SettlementDay").Value
StartMonth = Month(EndDate)
StartYear = Year(EndDate)
holidays = Array(NewYearsDay, MLKJrDay, PresidentsDay, MemorialDay, IndepDay, LaborDay, ColumbDay, _
VeterDay, ThanksgDay, ChristDay)
DateHolder = DateSerial(StartYear, StartMonth, 1)
newDate = WorksheetFunction.WorkDay(DateHolder, DayCount, holidays)
End Sub
我希望这可以帮助其他人将工作日功能纳入他们的VBA。