算某日期是某月的第几周感觉有难度啊,主要难度来自于"周跨月"的问题。
以下代码我在自已的电脑上试过,似乎输出结果还算是正确的。若有发现1)结果不对;2)代码有错;3)代码太啰索;4)其他问题,敬请帮我指出,多谢了。
Option Compare Database
Option Explicit
'以下均假设星期一为一个星期的第一天。若设星期日为一个星期的第一天,则相关代码相应调整。
Public Function wm()
Dim thedate As Date, n As Date, n0 As Integer, w As Integer, n1 As Integer, n2 As Integer
thedate = InputBox("日期(格式:YYYY-MM-DD)", "请输入:") '用INPUTBOX输入日期
n = DateValue(DateSerial(Year(thedate), Month(thedate), 1)) '返回当月第一天"
n0 = thedate - n
w = Weekday(n, vbMonday)
If w = 1 Then '当月第一天为星期一的情形
n1 = n0 \ 7 + 1
Else '当月第一天为星期二至星期日的情形
If n0 < (7 - w + 1) Then
n1 = 1
Else
n1 = (n0 - (7 - w + 1)) \ 7 + 2
End If
End If
n2 = DatePart("m", thedate) '取月份
MsgBox n2 & "月" & n1 & "周", vbOKOnly, "输入的日期是:" '用MSGBOX输出结果
End Function