如何使用vba在日期范围内循环几周

时间:2013-08-28 21:55:18

标签: vb.net vba excel-vba vbscript excel

我已经看过如何循环一年中的几周,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

3 个答案:

答案 0 :(得分:1)

您可以使用DateAdd function

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

答案 1 :(得分:0)

一天的整数值为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)

这适用于

答案 2 :(得分:0)

我尝试了这个解决方案,它似乎有效,我不是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