我想根据给定的日期计算一些时间表。就像我有
,我需要计算
weekly
biweekly
triweekly
monthly
quarterly
从开始日期和结束日期开始,以及通过匹配给定的工作日。
例如
Date start = 05/07/2018
Date End = 15/07/2018
Frequency days = Saturday
,我需要每周的星期六日期,然后是每两周的星期六日期,直到到达结束日期。
我曾在MS ACCESS VBA中尝试过DAYOFWEEK,这对您有所帮助,但我需要了解完整的解决方案,以便可以计算时间表。
感谢您的帮助。
谢谢
答案 0 :(得分:1)
DateAdd Function可以完成所有操作。
空号:
d = StartDate
Do While d <= EndDate
Debug.Print d ' <-- Output date
Select Case Interval
Case "biweekly": d = DateAdd("ww", 2, d)
Case "monthly" : d = DateAdd("m", 1, d)
' etc.
End Select
Loop
答案 1 :(得分:1)
对于几个月,您应该始终添加到原始开始日期,因为这可能是一个月的最后几天,因此会抵消日期较少的一个月后几个月的日期。所以:
table_name
将返回:
Dim StartDate As Date
Dim EndDate As Date
Dim NextDate As Date
Dim Interval As Long
StartDate = #1/31/2018#
EndDate = #6/30/2018#
Do
NextDate = DateAdd("m", Interval, StartDate)
Interval = Interval + 1
Debug.Print NextDate
Loop Until NextDate >= EndDate
要在特定的工作日开始,请找到该工作日的第一个,然后按照上述步骤添加时间间隔:
2018-01-31
2018-02-28
2018-03-31
2018-04-30
2018-05-31
2018-06-30
答案 2 :(得分:0)
这也应该起作用-我包括了输入框,因此您可以输入开始日期,结束日期,星期几和频率,因为我不知道您希望如何输入;这也会将值存储在Table2中,该表具有一个名为Dates的字段/列,然后您可以检索它们(我不知道您想如何检索日期,如果要存储它们,等等)。希望对您有所帮助!:
Sub test()
'clear the table2:
CurrentDb.Execute "DELETE * FROM Table2"
Dim DBTest As String
Dim RSTest As DAO.Recordset
Dim i As Long
Dim selectorInitDate, selectorEndDate, DBDate As Date
'Enter Start Date
selectorInitDate = Format(InputBox("Initial Date"), "mm/dd/yyyy")
'Enter Finish Date
selectorEndDate = Format(InputBox("End Date"), "mm/dd/yyyy")
'Enter Day of the Week (example: Saturday)
selectorWeekDay = InputBox("Week Day")
'Enter Frecuency (example: weekly, biweekly, etc)
selectorFreqDays = InputBox("Frecuency Days")
If selectorWeekDay = "Sunday" Then WeekDaySelected = 1
If selectorWeekDay = "Monday" Then WeekDaySelected = 2
If selectorWeekDay = "Tuesday" Then WeekDaySelected = 3
If selectorWeekDay = "Wednesday" Then WeekDaySelected = 4
If selectorWeekDay = "Thursday" Then WeekDaySelected = 5
If selectorWeekDay = "Friday" Then WeekDaySelected = 6
If selectorWeekDay = "Saturday" Then WeekDaySelected = 7
If selectorFreqDays = "weekly" Then Freq = 7
If selectorFreqDays = "biweekly" Then Freq = 14
If selectorFreqDays = "triweekly" Then Freq = 21
If selectorFreqDays = "monthly" Then Freq = 30
If selectorFreqDays = "quarterly" Then Freq = 90
DBDate = Format(selectorInitDate, "mm/dd/yyyy")
Count = 0
Do While DBDate <= selectorEndDate
If Weekday(DBDate) = WeekDaySelected Then
DBTest = "INSERT INTO Table2 ([Dates]) " & _
" VALUES (" & _
"'" & DBDate & "');"
CurrentDb.Execute DBTest
DBDate = DBDate + Freq - 1
Count = Count + 1
End If
DBDate = DBDate + 1
Loop
'this retrieves in a msgbox the saturdays found between the two dates you specify:
DBTest = "SELECT * FROM Table2"
Set RSTest = CurrentDb.OpenRecordset(DBTest)
If Not RSTest.BOF And Not RSTest.EOF Then
RSTest.MoveFirst
Do While (Not RSTest.EOF)
If Format(RSTest.Fields("Dates").Value, "mm/dd/yyyy") >= selectorInitDate And _
Format(RSTest.Fields("Dates").Value, "mm/dd/yyyy") <= selectorEndDate Then
mthString = mthString & RSTest.Fields("Dates") & ", "
End If
RSTest.MoveNext
Loop
End If
' (remove last comma)
mthString = Left(mthString, Len(mthString) - 2)
MsgBox Count & " " & selectorWeekDay & "(s) Added" & Chr(43) & mthString
'clear the table2:
CurrentDb.Execute "DELETE * FROM Table2"
End Sub
在您的示例之后,这应该为您提供每周两个日期之间有多少个星期六,以及那些日期。
注意:您需要在引用中选择“ Microsoft DAO 3.6对象库”