简体   繁体   English

VBA确定第N个特定日期的日期

[英]VBA Determine Date Of Nth Specific Day Of Month

Good Afternoon, 下午好,

I would like help with VBA code that will return the nth business date of a month while considering US Federal Holidays. 我需要有关VBA代码的帮助,该代码将在考虑美国联邦假日的同时返回一个月的第n个工作日期。 For instance, if I wanted to return the date for the 18th business day of January, 2014 the value should be 01/28/2014. 例如,如果我想返回2014年1月第18个工作日的日期,则该值应为01/28/2014。 There are two federal holidays in January which make the date be the 28th as opposed to the 24th. 一月有两个联邦假日,使该日期为28日,而不是24日。

The only code I was able to find was: 我唯一能找到的代码是:

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

This will find the 18th business day but does not take the holidays into consideration. 这将找到第18个工作日,但不考虑假期。

I do have code built that will return the dates for all the federal holidays within VBA. 我确实构建了代码,该代码将返回VBA中所有联邦假日的日期。 These values update automatically based on the current year. 这些值将根据当前年份自动更新。 I would prefer not to have to reference any table on the workbook. 我希望不必引用工作簿上的任何表格。

'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.

Any help showing how to build the code in VBA that accomplishes this would be appreciated. 任何帮助您展示如何在VBA中构建实现此目标的代码的帮助将不胜感激。

Thank you. 谢谢。

First, credit for this answer should go to Simoco . 首先,应该将此答案归功于Simoco It was his comments above that solved my problem. 正是他上面的评论解决了我的问题。 I wanted to mark this question answered and this was the only way I knew how. 我想将这个问题标记为已回答,这是我知道如何的唯一方法。

The short answer is Workday was the way to go for Excel 2007. If you are using Excel 2010, Workday or Workday_Intl will also work. 简短的答案是Excel 2007可以使用Workday 。如果您使用的是Excel 2010, WorkdayWorkday_Intl也可以使用。 Both opitions allow the user to enter holidays that should be excluded. 两种选择都允许用户输入不应排除的假期。 I used Dateserial for my date and built an array using the holiday dates I had calculated earlier. 我使用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

I hope this helps anyone else having difficulty incorporating workday function into their VBA. 我希望这可以帮助其他任何难以将工作日功能集成到其VBA中的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM