【发布时间】: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
【问题讨论】: