如何使用vba在日期范围内循环显示周数



我已经看到了如何循环一年中的周,w1301,w1302,w1303,如果我在周号上循环+,我可以得到周号,但我相信有一种方法可以直接用vba每周循环,我希望至少是这样。

   DateSerial(Year(Now), Month(Now), Day(Now)) To DateSerial(2013, 3, 1)
    StartDate = #1/1/2013#
    EndDate = #12/31/2013#
  For DateLooper = StartDate To EndDate

我得到了一个星期号码的功能,从日期

     Public Function IsoWeekNumber(d1 As Date) As Integer
     Attributed to Daniel Maher
     Dim d2 As Long
     d2 = DateSerial(Year(d1 - WeekDay(d1 - 1) + 4), 1, 3)
     IsoWeekNumber = Int((d1 - d2 + WeekDay(d2) + 5) / 7)
     End Function

您可以使用DateAdd函数

For i = 1 To 52  
    Debug.Print DateAdd("ww", i, Now())  
Next i

一天的整数值为1,因此您可以按周进行迭代,如下所示:

startDate = #1/1/2013#
endDate   = #12/31/2013#
For d = startDate To endDate Step 7
  'do stuff
Next

周数可通过DatePart功能确定,例如:

WScript.Echo DatePart("ww", Now)

这将在vbscript和vba中都有效。

我尝试了这个解决方案,它似乎有效,我不能100%确定它如何处理不同月份的28,30,31天,但我信任vba。我知道我可能犯了一个错误:)

  currentDate = "2013-01-02"    ' coz i wanted to start on a wednesday
  for week = 1 to 52
  debug.print currentDate
  currentDate = DateAdd("ww",1,currentDate)
  next week

最新更新