VBA确定月的第N个特定date的date

下午好,

我想帮助VBA代码,将返回一个月的第n个营业date,同时考虑美国联邦假期。 例如,如果我想要返回2014年1月18日的date,则值应为01/28/2014。 一月份有两个联邦法定节假日,第二十八天是第二十四天。

我能find的唯一代码是:

finaldate = Application.Evaluate("workday(Date(2014,01,0),18)") 

这将find第18个工作日,但不考虑节假日。

我有build立的代码将返回VBA内所有联邦假期的date。 这些值根据当年自动更新。 我不希望引用工作簿上的任何表格。

 '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中构build完成此代码将不胜感激。

谢谢。

首先,这个答案的信贷应该去Simoco 。 这是他上面的意见,解决了我的问题。 我想要回答这个问题,这是我知道的唯一方法。

简单的答案是Workday是Excel 2007的发展方向。如果您使用的是Excel 2010, WorkdayWorkday_Intl也可以工作。 这两种select都允许用户input应该被排除的假期。 我使用Dateserial作为我的date,并使用之前计算的假期date构build了一个数组。

 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 

我希望这可以帮助其他人将工作日function整合到他们的VBA中。