新年日期之间的周数问题

时间:2019-07-08 17:45:18

标签: excel vba calendar week-number

问题

以下[mcve]将输出两个日期之间的星期数数组。当两个日期都在同一年时,它就可以工作,但是,有些年份有52个星期,并且从去年的最后几天开始。其他人有53周。

2020 calendar是一个52周的例子:

Jan 2020 Calendar

第一个星期从12月30日开始。

53周的示例是2016 calendar

Jan 2016 Calendar

那只在1月4日开始。

代码

下面的代码被注释并输出包含星期数的数组。

Sub w_test()
    Dim Arr() As Variant, ArrDateW() As Variant
    'Initial Date
    DateI = DateSerial(2015, 5, 5)
    'Final Date
    DateF = DateSerial(2017, 9, 20)
    'Difference in weeks between DateI and DateF
    weekDif = DateDiff("ww", DateI, DateF) + k - 1

    i = Weekday(DateI)
    d = DateI

    'If not Sunday, go back to last week, to start the loop
    If i <> 1 Then
        d = DateAdd("d", -(i - 1), d)
    End If

    ReDim ArrDateW(weekDif)
    ReDim Arr(2)
    'Loop on all weeks between two dates to populate array of arrays
    For i = 0 To weekDif
        'Date
        Arr(0) = d
        'Trying to solve problem with New Year
        If Application.WorksheetFunction.WeekNum(d) = 53 Then
            flag = True
        End If
        If flag = False Then
            Arr(1) = Application.WorksheetFunction.WeekNum(d)
        Else
            Arr(1) = Application.WorksheetFunction.WeekNum(DateSerial(Year(d) + 1, 1, 1))
            flag = False
        End If

        'Year
        Arr(2) = Year(d)
        'Populate array of arrays
        ArrDateW(i) = Arr
        'Next Week Number
        d = DateAdd("ww", 1, d)
    Next i

    'To stop with Ctrl+F8
    Debug.Print d
End Sub

问题

2015年有53周,但是程序输出以下内容:

Output Local Variable

2016 and 2017之间,输出是一团糟:

Output Local Variable

如何修复程序以正确输出这些星期数?

1 个答案:

答案 0 :(得分:1)

我依靠内置的VBA函数正确地计算周数,因此处理方式有所不同。了解有关ISO周数的信息是this answer,并了解我如何使用DataPart函数-尽管您可以根据需要替换自己的Ron de Bruin's ISO week number function版本。

一些简短的注释:

  1. Always use Option Explicit
  2. 尝试使用更具描述性的变量名称。您现在知道您在说什么。再过几个月,您将很难记住dArr的含义(即使现在看起来很明显)。这只是一个好习惯,并且可以使代码具有自说明性。
  3. 下面的示例将逻辑分为一个带有可选参数(仅出于娱乐目的)的单独函数,该参数允许调用者将一周的开始更改为另一天。

代码模块:

Option Explicit

Sub w_test()
    Dim initialDate As Date
    Dim finaldate As Date
    initialDate = #5/5/2015#
    finaldate = #9/29/2017#

    Dim weeks As Variant
    weeks = WeekNumbers(initialDate, finaldate)

    Debug.Print "There are " & UBound(weeks, 1) & " weeks between " & _
                Format(initialDate, "dd-mmm-yyyy") & " and " & _
                Format(finaldate, "dd-mmm-yyyy")
End Sub

Private Function WeekNumbers(ByVal initialDate As Date, _
                             ByVal finaldate As Date, _
                             Optional ByVal weekStart As VbDayOfWeek = vbSunday) As Variant
    Dim numberOfWeeks As Long
    numberOfWeeks = DateDiff("ww", initialDate, finaldate, weekStart, vbFirstFullWeek)

    Dim startOfWeek As Date
    If Weekday(initialDate) <> vbSunday Then
        Dim adjustBy As Long
        If Weekday(initialDate) > weekStart Then
            adjustBy = Weekday(initialDate) - weekStart
        Else
            adjustBy = (Weekday(initialDate) + 7) - weekStart
        End If
        startOfWeek = DateAdd("d", -adjustBy, initialDate)
    End If

    Dim allTheWeeks As Variant
    ReDim allTheWeeks(1 To numberOfWeeks)

    Dim weekInfo As Variant
    ReDim weekInfo(1 To 3)

    Dim i As Long
    For i = 1 To numberOfWeeks
        weekInfo(1) = startOfWeek
        weekInfo(2) = DatePart("ww", startOfWeek, weekStart, vbFirstFourDays)
        weekInfo(3) = Year(startOfWeek)
        allTheWeeks(i) = weekInfo
        startOfWeek = DateAdd("ww", 1, startOfWeek)
    Next i

    WeekNumbers = allTheWeeks
End Function