如果您不想要日历周,并且想要包括一周,即使其中有一天,那么您可以试试这个。
逻辑
- 它会查找下一周的开始时间。所以默认的周数是
1。
- 循环并计算两个日期之间的天数(例如 Monday,然后是 Tuesday 等等),以最高者为准,将其返回并将其添加到 @ 987654323@。
也不需要循环和创建数组。您可以在 One Go
中创建顺序数组
代码
Option Explicit
Sub Sample()
Dim WeeksCount As Long
Dim CurCount As Long
Dim countOfWeeks As Long
Dim i As Long, tmpCount As Long
Dim sDate As Date, eDate As Date
sDate = DateSerial(2019, 1, 6)
eDate = DateSerial(2019, 1, 8)
If Weekday(sDate, vbMonday) <> 1 Then
sDate = DateAdd("d", 7 - Weekday(sDate, vbMonday) + 1, sDate)
WeeksCount = 1
End If
For i = 1 To 7
CurCount = GetMeMyKindOfWeeksTotal(sDate, eDate, i)
If tmpCount < CurCount Then tmpCount = CurCount
Next i
WeeksCount = WeeksCount + tmpCount
Dim MyArray
MyArray = Evaluate("Row(1" & ":" & WeeksCount & ")")
If WeeksCount = 1 Then
Debug.Print MyArray(1)
Else
For i = LBound(MyArray) To UBound(MyArray)
Debug.Print MyArray(i, 1)
Next i
End If
End Sub
Private Function GetMeMyKindOfWeeksTotal(ByVal sDate As Date, ByVal eDate As Date, dy As Long)
Dim j As Long
Dim TotalDays As Long
For j = sDate To eDate
If Weekday(j) = dy Then
TotalDays = TotalDays + 1
End If
Next
GetMeMyKindOfWeeksTotal = TotalDays
End Function
各种测试
sDate = DateSerial(2019, 1, 6)
eDate = DateSerial(2019, 1, 8)
这会给你2
sDate = DateSerial(2019, 1, 1)
eDate = DateSerial(2019, 3, 1)
这会给你9
sDate = DateSerial(2019, 1, 1)
eDate = DateSerial(2019, 1, 1)
这会给你1
sDate = DateSerial(2019, 1, 1)
eDate = DateSerial(2019, 1, 8)
这会给你2