获取前一周周一至周五的日期
Get dates of Mon-Fri of previous week
我正在尝试实施 this SO answer 以获取前一周的周一至周五的日期,并以此进行测试:
Public Sub listPreviousWeekDays()
Dim dayCtr As Long
For dayCtr = 2 To 6
Debug.Print DateAdd("ww", -1, Now - (Weekday(Now, dayCtr) - 1))
Next
End Sub
然而,这给出了:
4/10/2017 9:27:18 AM
4/11/2017 9:27:18 AM
4/12/2017 9:27:18 AM
4/6/2017 9:27:18 AM
4/7/2017 9:27:18 AM
我的目标是 return:
4/10/2017 9:27:18 AM
4/11/2017 9:27:18 AM
4/12/2017 9:27:18 AM
4/13/2017 9:27:18 AM
4/14/2017 9:27:18 AM
编辑:为了避免 XY problem,我将解释我正在尝试做的事情:我想 运行 一个用前一周的星期一填充单元格块的宏-周五日期。
已更新
正如 OP 指出的那样,当开始日期是星期日时,链接代码不适用于此特定用例。这是一个修复:
Option Explicit ' always always always use this
Public Function MondayLastWeek(pdat As Date) As Date
Dim weekday_increment As Long
weekday_increment = weekday(pdat, vbMonday) - 1
MondayLastWeek = DateAdd("ww", IIf(weekday_increment = 6, 0, -1), pdat - weekday_increment)
' IIf: if we're on Sunday, pdat-weekday_increment is already the date we want.
End Function
Public Sub listPreviousWeekDays()
Dim monday As Date
monday = MondayLastWeek(Now)
Dim dayCtr As Long
For dayCtr = 0 To 4
Debug.Print monday + dayCtr
Next
End Sub
原回答
这对我有用:
Public Sub listPreviousWeekDays()
' First, get last week's Monday just as in the linked question
Dim LastMonday As Date
LastMonday = DateAdd("ww", -1, Now - (Weekday(Now, vbMonday) - 1))
' Then, loop over the rest of the week.
' Monday + 0 days => Monday
' Monday + 4 days => Friday
Dim dayCtr As Long
For dayCtr = 0 To 4
Debug.Print LastMonday + dayCtr
Next
End Sub
输出:
4/10/2017 12:38:17 PM
4/11/2017 12:38:17 PM
4/12/2017 12:38:17 PM
4/13/2017 12:38:17 PM
4/14/2017 12:38:17 PM
我认为问题是 Weekday
的第二个参数没有指定您 想要 的哪一天。相反,它指定一周中的哪一天是 工作日编号 1(some documentation). Instead of messing with Weekday
, just get the Monday using the linked answer,然后根据需要添加天数。
这应该可以解决问题。
Public Sub listPreviousWeekDays()
Dim dayCtr As Long
For dayCtr = 2 To 6
Debug.Print DateAdd("ww", -1, Now - (Weekday(Now, vbUseSystemDayOfWeek)) + dayCtr)
Next
End Sub
我正在尝试实施 this SO answer 以获取前一周的周一至周五的日期,并以此进行测试:
Public Sub listPreviousWeekDays()
Dim dayCtr As Long
For dayCtr = 2 To 6
Debug.Print DateAdd("ww", -1, Now - (Weekday(Now, dayCtr) - 1))
Next
End Sub
然而,这给出了:
4/10/2017 9:27:18 AM
4/11/2017 9:27:18 AM
4/12/2017 9:27:18 AM
4/6/2017 9:27:18 AM
4/7/2017 9:27:18 AM
我的目标是 return:
4/10/2017 9:27:18 AM
4/11/2017 9:27:18 AM
4/12/2017 9:27:18 AM
4/13/2017 9:27:18 AM
4/14/2017 9:27:18 AM
编辑:为了避免 XY problem,我将解释我正在尝试做的事情:我想 运行 一个用前一周的星期一填充单元格块的宏-周五日期。
已更新
正如 OP 指出的那样,当开始日期是星期日时,链接代码不适用于此特定用例。这是一个修复:
Option Explicit ' always always always use this
Public Function MondayLastWeek(pdat As Date) As Date
Dim weekday_increment As Long
weekday_increment = weekday(pdat, vbMonday) - 1
MondayLastWeek = DateAdd("ww", IIf(weekday_increment = 6, 0, -1), pdat - weekday_increment)
' IIf: if we're on Sunday, pdat-weekday_increment is already the date we want.
End Function
Public Sub listPreviousWeekDays()
Dim monday As Date
monday = MondayLastWeek(Now)
Dim dayCtr As Long
For dayCtr = 0 To 4
Debug.Print monday + dayCtr
Next
End Sub
原回答
这对我有用:
Public Sub listPreviousWeekDays()
' First, get last week's Monday just as in the linked question
Dim LastMonday As Date
LastMonday = DateAdd("ww", -1, Now - (Weekday(Now, vbMonday) - 1))
' Then, loop over the rest of the week.
' Monday + 0 days => Monday
' Monday + 4 days => Friday
Dim dayCtr As Long
For dayCtr = 0 To 4
Debug.Print LastMonday + dayCtr
Next
End Sub
输出:
4/10/2017 12:38:17 PM
4/11/2017 12:38:17 PM
4/12/2017 12:38:17 PM
4/13/2017 12:38:17 PM
4/14/2017 12:38:17 PM
我认为问题是 Weekday
的第二个参数没有指定您 想要 的哪一天。相反,它指定一周中的哪一天是 工作日编号 1(some documentation). Instead of messing with Weekday
, just get the Monday using the linked answer,然后根据需要添加天数。
这应该可以解决问题。
Public Sub listPreviousWeekDays()
Dim dayCtr As Long
For dayCtr = 2 To 6
Debug.Print DateAdd("ww", -1, Now - (Weekday(Now, vbUseSystemDayOfWeek)) + dayCtr)
Next
End Sub