Excel: Custom VBA Function to Return Quarter from Date

Posted: August 22, 2017 at 4:35 pm, Last Updated: August 22, 2017 at 4:36 pm


Function fnQuarterFromWeek(varDate)

'Declare variables
Dim varMonth

'Get month from date
varMonth = Month(varDate)

'Set quarter based on month
If varMonth < 3 Then varQuarter = 1 If varMonth > 3 Then varQuarter = 2
If varMonth > 6 Then varQuarter = 3
If varMonth > 9 Then varQuarter = 4

'Return value
fnQuarterFromWeek = Year(varDate) & "-" & varQuarter

End Function