【问题标题】:Excel VBA count non business days/weekend daysExcel VBA计数非工作日/周末
【发布时间】:2019-01-22 14:30:06
【问题描述】:

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

背景:

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

代码应该做什么:

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

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

请解释所有 VBA 技能水平的代码 cmets 的任何响应。 欢迎代码和逻辑帮助。 **

'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

【问题讨论】:

    标签: excel vba


    【解决方案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
    

    【讨论】:

    • 感谢您的快速回复。我不认为上面的代码是在 VBA 中。我可以遵循一些逻辑,但我认为它不适用于 VBA 类结构。
    【解决方案2】:

    执行此操作的最直观的方法(在我看来)是简单地逐一计算前进的天数,直到您添加了用户请求的工作日数。无论如何都不需要 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
    

    【讨论】:

    • 感谢您的快速回复。这段代码正是我正在尝试做的,只是在与它斗争的公共汽车上大声笑。感谢您提供所有代码内 cmets 并保持问题中的代码风格。
    • 很高兴。请注意,这段代码效率很低,只是相对容易理解。从理论上讲,您可以在循环时忽略除第一个和最后一个之外的所有周,因此如果用户输入 100 亿天,它会无缘无故地运行很长时间。但正如你所说,你的限制是 14,没关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2018-12-31
    • 2017-08-14
    相关资源
    最近更新 更多