Mason Web Analytics

Excel: Custom VBA Function to Return Quarter from Date


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