计算特定年份的总周数 - ISO 8601 - VBA 访问

Calculate total of weeks in a specific year - ISO 8601 - VBA Access

多次看到这个问题,但似乎无法在我的 VBA 代码中修复它。

我需要根据 ISO 8601 计算给定年份的总周数。

当我使用 datediff 函数时:iNumWeeks = DateDiff("ww", "1/1/2015", "31/12/2015", vbMonday, vbFirstJan1) 它 returns 52 而 2015 年有 53 周 (ISO 8601)

我怎样才能完成这项工作?

如果您不介意使用 UDF,此代码将 return 它。
=ISOWeekNum("31/12/2015")=ISOWeekNum(42369) returns 53.

http://www.cpearson.com/excel/DateTimeVBA.htm

'http://www.cpearson.com/excel/DateTimeVBA.htm
'John Green, an Excel MVP from Australia.
Public Function ISOWeekNum(AnyDate As Date, Optional WhichFormat As Variant) As Integer
' WhichFormat: missing or <> 2 then returns week number,
'                                = 2 then YYWW
'
Dim ThisYear As Integer
Dim PreviousYearStart As Date
Dim ThisYearStart As Date
Dim NextYearStart As Date
Dim YearNum As Integer

ThisYear = Year(AnyDate)
ThisYearStart = YearStart(ThisYear)
PreviousYearStart = YearStart(ThisYear - 1)
NextYearStart = YearStart(ThisYear + 1)
Select Case AnyDate
    Case Is >= NextYearStart
        ISOWeekNum = (AnyDate - NextYearStart) \ 7 + 1
        YearNum = Year(AnyDate) + 1
    Case Is < ThisYearStart
        ISOWeekNum = (AnyDate - PreviousYearStart) \ 7 + 1
        YearNum = Year(AnyDate) - 1
    Case Else
        ISOWeekNum = (AnyDate - ThisYearStart) \ 7 + 1
        YearNum = Year(AnyDate)
End Select

If IsMissing(WhichFormat) Then Exit Function
If WhichFormat = 2 Then
    ISOWeekNum = CInt(Format(Right(YearNum, 2), "00") & _
    Format(ISOWeekNum, "00"))
End If

End Function

Public Function YearStart(WhichYear As Integer) As Date

Dim WeekDay As Integer
Dim NewYear As Date

NewYear = DateSerial(WhichYear, 1, 1)
WeekDay = (NewYear - 2) Mod 7 'Generate weekday index where Monday = 0

If WeekDay < 4 Then
    YearStart = NewYear - WeekDay
Else
    YearStart = NewYear - WeekDay + 7
End If

End Function

可以在查询中用作: SELECT DISTINCT dDate, ISOWeekNum(dDate) 来自 tbl_MyTable

DatePart,你试过这个吗

iNumWeeks = DatePart("ww", CDate("31/12/2015"), vbMonday, vbFirstFourDays) '53

这是一个简单的函数,可以提供任何年份的正确结果:

Public Function ISO_WeekCount( _
    ByVal datYear As Date) _
    As Byte

' Calculates number of weeks in year of datYear according to the ISO 8601:1988 standard.
'
' May be freely used and distributed.
' 2001-06-26. Gustav Brock, Cactus Data ApS, CPH

    Dim bytISO_Thursday As Byte

    ' No special error handling.
    On Error Resume Next

    bytISO_Thursday = Weekday(vbThursday, vbMonday)

    datYear = DateSerial(Year(datYear), 12, 31)
    ' Subtract one week if datYear is in week no. 1 of next year.
    datYear = DateAdd("ww", Weekday(datYear, vbMonday) < bytISO_Thursday, datYear)

    ISO_WeekCount = DatePart("ww", datYear, vbMonday, vbFirstFourDays)

End Function

引用自https://en.wikipedia.org/wiki/ISO_8601#Week_dates

28 December is always in the last week of its year.

这意味着它真的和

一样简单
Public Function WeeksInYear(lYear As Long) As Long
    
    WeeksInYear = DatePart("ww", DateSerial(lYear, 12, 28), vbMonday, vbFirstFourDays)

End Function