Excel VBA计算非工作日/周末天

时间:2018-08-15 17:11:55

标签: excel-vba

**大家好, 我有一个问题,关于代码的½和关于代码背后的逻辑的½。

背景:

这是大型工作簿集合中一个Sub的很小一部分。此代码段的目标是接受用户希望输入日期范围的工作日数的输入。确定两个日期之间是否包含周末,如果是,则将2加上该范围。输入是数据类型Integer。该数字会添加到当前日期中,以获取该范围内的最后一个日期,并分配给dDate,以便在此Sub和其他Sub中使用。

代码应该做什么:

用户最多可以请求查找的是14(不需要错误处理就可以保存14个以上)。可以在一周中的任何一天(包括周末)提出请求。如果要求在星期三查询3个工作日,则程序应添加2以显示星期四,星期五,星期六,星期日和星期一。如果要求在星期六显示2个工作日,则该程序应加1以显示星期日,星期一和星期二。如果要求的数字在范围(8-14)之间有2个周末,则加4。

简而言之,对于日期范围内的每个周末,将一天添加到用户输入数字中。

对于所有VBA技能水平,请在代码注释中说明所有答案。 欢迎使用代码和逻辑帮助。 **

'prompt to enter number of days to look out for shortage, new addition to the code added to expand usability
iNumDays = Application.InputBox(prompt:="Enter number of business days to look out for")

iweekday = Weekday(Date, vbMonday) 'get todays weekday number 1-7 with Monday being 1, Sunday being 7

'if today is Thursday or Friday the next 2 business days fall on the weekend, if so then we need to look out 2 days more
If iweekday > 3 Then 'iweekday is integer of todays weekday number, if its past Wednesday then enter If

    iNumDays = iNumDays + 2 'add 2 to user input
End If

dDate = Date + iNumDays    'add user day to look out input to todays date to get last date in desired date range             'get the column header for the date we are looking out to

2 个答案:

答案 0 :(得分:1)

解决方案在这里找到:https://www.experts-exchange.com/questions/23461938/VB-net-Add-Days-to-a-Date.html

Public Function AddNBusinessDays(ByVal startDate As DateTime, ByVal numDays As Integer) As DateTime

    If numDays = 0 Then Return New DateTime(startDate.Ticks)

    If numDays < 0 Then Throw New ArgumentException()

    Dim i As Integer
    Dim totalDays As Integer
    Dim businessDays As Integer

    totalDays = 0
    businessDays = 0

    Dim currDate As DateTime
    While businessDays < numDays
        totalDays += 1

        currDate = startDate.AddDays(totalDays)

        If Not (currDate.DayOfWeek = DayOfWeek.Saturday Or currDate.DayOfWeek = DayOfWeek.Sunday) Then
            businessDays += 1
        End If

    End While

    Return currDate

End Function

答案 1 :(得分:0)

最直观的方法(我认为)是简单地一一算出天数,直到您增加了用户要求的工作日数。无论如何,这14天的限制是不必要的,因为它是一个循环,可以处理长达数十亿天的任何整数...

Sub adddays()

Dim iNumDays As Integer
Dim iWeekDay As Integer
Dim dDate As Date

'prompt to enter number of days to look out for shortage, new addition to the code added to expand usability
iNumDays = Application.InputBox(prompt:="Enter number of business days to look out for")

dDate = Date                            ' initialize dDate with today's date before entering the counting loop

While iNumDays > 0                      ' as long as the there are still workdays left to add, repeat this
    dDate = dDate + 1                   ' move calendar forward by one day
    iWeekDay = Weekday(dDate, vbMonday) ' check what weekday we arrived at
    If iWeekDay < 6 Then                ' if we're looking at a working day, we successfully added one of the desired weekdays to be added
        iNumDays = iNumDays - 1
    End If
Wend

MsgBox ("Target date is: " & Str(dDate)) 'check results of the calculation or replace with whatever other logic you want here

End Sub