VBA Function to get the financial week number - year starting on July

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. 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
1627366291581.png


2021 -> 2020
1627366477326.png


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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
what if you change this code to check for <=53

If WeeksInYear <= 53 Then 'Number of weeks in the year
 
Upvote 0
The WEEKNUM function is correct given a week would start on Sunday for the dates you list above. If you change the dates to 1/1/yy you will see that the WEEKNUM returns 1. This is because the dates fall on a Friday or Saturday respectively and the new week (#2) starts on Sunday.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top