使用VBA是否可以获得当月的第一个星期一?我原以为下面会有用: -
DateSerial(Year(Date), Month(Date), ((7 - Weekday(DateSerial(Year(Date), Month(Date), 7))) + 2) Mod 7)
...但如果我今天使用这个例子,我得到的日期是2017年7月31日,这显然是上个月。
答案 0 :(得分:2)
编辑: 这是一个可能的答案(so the first one in google is a false one):
Public Function FirstMonday(myDate As Date) As Date
Dim d As Date, w As Long
d = DateSerial(Year(myDate), month(myDate), 1)
w = Weekday(d, vbMonday)
FirstMonday = d + IIf(w <> 1, 8 - w, 0)
End Function
这就是你所说的:
Public Sub Test
debug.print FirstMonday(Now)
End sub
一般来说,这是Excel Date Functions的有趣读物。
答案 1 :(得分:0)
您可以使用此通用功能:
FirstMonday = DateWeekdayInMonth(Date, 1, vbMonday)
然后:
SELECT * FROM tags, sentenceTags, sentences WHERE tags.name = 'books' AND tags.id = sentenceTags.tagId AND sentences.id = sentenceTags.sentenceId
答案 2 :(得分:0)
运行以下宏,它会为您提供本月的第一个星期一:
Sub PromptFirstMondayofTheCurrentMonth()
Dim a As Variant
a = Date - Day(Date) + 1
MsgBox IIf(Weekday(a, vbMonday) = 1, 1, 9 - Weekday(a, vbMonday))
End Sub