実際の週と会計(財務)週のどちらを参照しているかに応じて、次の2つの関数のいずれかを使用して計算できます。
会計週:
Function FiscalWeek(dtDate As Variant)
Dim dtFiscal As Date
Dim dtFstMon As Date
Dim i As Integer
'Ensure we are working with a valid date
If Not IsDate(dtDate) Then
FiscalWeek = "Invalid Date"
Exit Function
End If
'Strip out any timestamps
dtFiscal = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))
'Determine first Monday of April for given year
i = 1
Do Until Weekday(DateSerial(Year(dtFiscal), 4, i), 2) = 1
i = i + 1
Loop
dtFstMon = DateSerial(Year(dtFiscal), 4, i)
'Work out the difference in weeks from April for the relevant year
Select Case dtFiscal
Case Is < dtFstMon
FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal) - 1, 4, 1), dtFiscal, 2)
Case Else
FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal), 4, 1), dtFiscal, 2) + IIf(i = 1, 1, 0)
End Select
End Function
または実際の週:
Function ActualWeek(dtDate As Variant)
Dim dtActual As Date
Dim dtFstMon As Date
Dim i As Integer
'Ensure we are working with a valid date
If Not IsDate(dtDate) Then
ActualWeek = "Invalid Date"
Exit Function
End If
'Strip out any timestamps
dtActual = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))
'Determine first Monday of January for given year
i = 1
Do Until Weekday(DateSerial(Year(dtActual), 1, i), 2) = 1
i = i + 1
Loop
dtFstMon = DateSerial(Year(dtActual), 1, i)
'Work out the difference in weeks from January for the relevant year
Select Case dtActual
Case Is < dtFstMon
ActualWeek = DateDiff("WW", DateSerial(Year(dtActual) - 1, 1, 1), dtActual, 2)
Case Else
ActualWeek = DateDiff("WW", DateSerial(Year(dtActual), 1, 1), dtActual, 2) + IIf(i = 1, 1, 0)
End Select
End Function