未經測試。可能改進。根據您的要求對代碼進行調整。
Public Function DateOfFirstDayofWeek(intCurrentDayofWeek As Integer, WhichDate As Date) As Date
DateOfFirstDayofWeek = DateAdd("D", intCurrentDayofWeek * (-1) + 2, WhichDate)
End Function
Function dhFirstDayInMonth(dtmDate As Date) As Date
dhFirstDayInMonth = DateSerial(year(dtmDate), Month(dtmDate), 1)
End Function
Function dhLastDayInMonth(dtmDate As Date) As Date
dhLastDayInMonth = DateSerial(year(dtmDate), Month(dtmDate) + 1, 0)
End Function
Private Sub getWeekRange_Click()
Dim startDate As Date
Dim endDate As Date
Dim tmpDate As Date
Dim dateOfMonth As Date
Dim myDate As Date
dateOfMonth = CDate("01/02/2013")
endDate = dhLastDayInMonth(dateOfMonth)
startDate = dhFirstDayInMonth(dateOfMonth)
tmpDate = startDate
While tmpDate < endDate
myDate = DateOfFirstDayofWeek(Weekday(tmpDate, vbSunday), tmpDate) ''Assuming Week starts with Sunday
tmpDate = DateAdd("d", 6, myDate)
If myDate < startDate Then
myDate = startDate
ElseIf tmpDate > endDate Then
tmpDate = endDate
End If
Debug.Print myDate & " " & tmpDate 'This Prints the Start date and End date of every week
Wend
End Sub
@Bayusrihernogo,你試過這個,有用嗎? – rags
非常有用謝謝^^ –
@ it.bayu,你需要接受幫助你的答案。通過單擊答案的上/下投票控件下方的刻度標記可以接受答案 – rags