Guinaba
Board Regular
- Joined
- Sep 19, 2018
- Messages
- 235
- Office Version
- 2016
- Platform
- Windows
Hi guys,
Trying to create a function that gives me the financial week number based on the week number, the financial week number start on July. However I keep getting the incorrect week number everytime the new year starts, for example in the pic below the Wk# is jumping from wk53 (2020) to wk2 when moving to new year, maybe week number is not a good idea to get the financial week number. My weeks start on Sundays. Any suggestions?
2020 -> 2021
2021 -> 2020
Trying to create a function that gives me the financial week number based on the week number, the financial week number start on July. However I keep getting the incorrect week number everytime the new year starts, for example in the pic below the Wk# is jumping from wk53 (2020) to wk2 when moving to new year, maybe week number is not a good idea to get the financial week number. My weeks start on Sundays. Any suggestions?
2020 -> 2021
2021 -> 2020
VBA Code:
Option Explicit
Function FWkNum(InputDate As Date) As String
Dim WeeksInYear As Long
WeeksInYear = DatePart("ww", DateSerial(Year(InputDate), 12, 28), vbSunday, vbFirstFourDays) '28 December is always in the last week of its year.
Dim InWk As Long
InWk = WorksheetFunction.WeekNum(InputDate, vbSunday)
If WeeksInYear <= 52 Then 'Number of weeks in the year
If InWk <= 26 Then 'Wks before the financial year (Jul)
FWkNum = WorksheetFunction.WeekNum(InputDate, vbMonday) + 26
Else
FWkNum = WorksheetFunction.WeekNum(InputDate, vbMonday) - 25
End If
Else 'year with 53 weeks
If InWk <= 26 Then 'Wks before the financial year (Jul)
FWkNum = WorksheetFunction.WeekNum(InputDate, vbMonday) + 26
Else
FWkNum = WorksheetFunction.WeekNum(InputDate, vbMonday) - 25
End If
End If
End Function
Sub Test()
Dim InputDate As Date
Dim WeeksInYear As Long
InputDate = ActiveSheet.Range("d29").Value
ActiveSheet.Range("f29").Value = FWkNum(InputDate)
End Sub