How to loop through the weeks in a date range with vba

 ̄綄美尐妖づ 提交于 2020-07-24 04:54:48

问题


I have seen how to loop through weeks of a year, w1301,w1302,w1303, I can get the week number if i loop through + on week number but I believe there is a way to directly loop weekly with vba, i hope at least.

   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

I got the function for a week number from date

     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

回答1:


You could just use the DateAdd function

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



回答2:


A day has an integer value of 1, so you could iterate by week like this:

startDate = #1/1/2013#
endDate   = #12/31/2013#

For d = startDate To endDate Step 7
  'do stuff
Next

The week number can be determined with the DatePart function, e.g.:

WScript.Echo DatePart("ww", Now)

This will work in both vbscript and vba.




回答3:


I tried this solution and it seems to work, am not 100% sure of how it handles the 28,30,31 days of different months but i trust vba. i know am making a mistake probably :))

  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


来源:https://stackoverflow.com/questions/18498885/how-to-loop-through-the-weeks-in-a-date-range-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!