title: Problem with Week Numbers between dates on New Year
body: <h2>Problem</h2>
  
  <p>The following [mcve] will output an array of arrays of week numbers between two dates. It works when both dates are on the same year, however, some years have 52 weeks and start within the last days of the last year. And others have 53 weeks.</p>
  
  <p>An example of 52 weeks is the <a href="http://week-number.net/calendar-with-week-numbers-2020.html" rel="nofollow noreferrer">2020 calendar</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/pfLji.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pfLji.png" alt="Jan 2020 Calendar"></a></p>
  
  <p>Where the first week begins on Dec 30.</p>
  
  <p>And the example of 53 weeks is the <a href="http://week-number.net/calendar-with-week-numbers-2016.html" rel="nofollow noreferrer">2016 calendar</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/gGssd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gGssd.png" alt="Jan 2016 Calendar"></a></p>
  
  <p>That begins only on Jan 4th.</p>
  
  <h2>Code</h2>
  
  <p>The following code is commented and outputs an array of arrays with the week numbers.</p>
  
  <pre><code>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 &lt;&gt; 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
  </code></pre>
  
  <h2>Question</h2>
  
  <p>2015 had 53 weeks, however the program outputs the following:</p>
  
  <p><a href="https://i.stack.imgur.com/3AKO9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3AKO9.png" alt="Output Local Variable"></a></p>
  
  <p>And between <a href="http://week-number.net/calendar-with-week-numbers-2016.html" rel="nofollow noreferrer">2016 and 2017</a>, the output is a mess:</p>
  
  <p><a href="https://i.stack.imgur.com/e25wa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e25wa.png" alt="Output Local Variable"></a></p>
  
  <h3>How to fix the program to output these week numbers correctly?</h3>