【发布时间】:2021-07-29 02:18:24
【问题描述】:
我正在尝试获取当月星期一的所有日期。例如,在 2021 年 5 月的当前月份,我们将有:3/05/2021、10/05/2021、17/05/2021、24/05/2021、31/05/2021。
在调查中,我发现了一个旧问题的答案,这有助于 Calculate the number of weeks in a month,但是这显示 6 作为答案。哪个是正确的(请参阅共享日历)。但是我希望只计算该月的星期一。
我还有一个补充代码,可以告诉我当月的星期一数:
Sub NumMondays()
Dim i As Integer
Dim num_mondays As Integer
Dim test_date As Date
Dim orig_month As Integer
month_name = Format(Date, "mmmm")
year_name = Format(Date, "yyyy")
' Get the first day of the month.
test_date = CDate(month_name & " 1, " & year_name)
' Count the Mondays.
orig_month = Month(test_date)
Do
num_mondays = num_mondays + 1
test_date = DateAdd("ww", 1, test_date)
Loop While (Month(test_date) = orig_month)
Debug.Print test_date
Debug.Print orig_month
Debug.Print num_mondays
End Sub
这样的代码打印 5 表示星期一的数量,但是我无法将其转换为此类星期一的实际日期。有什么建议吗?
提前非常感谢
【问题讨论】:
标签: excel vba date calculated-field