Access VBA Get Quarter From Month Number
Asked Answered
H

4

5

I am stuck in trying to find the quarter of year number from the month number i.e. i have month number 2 which should be quarter number 1, how do i do this in Access VBA or an access query? Thanks in advance.

Heartburn answered 17/10, 2014 at 13:54 Comment(2)
You could use Choose(MonthNum,1,1,1,2,2,2,3,3,3,4,4,4)Gouda
@Gouda you should throw that up as an answer. The only thing it lacks is error/out of bounds handling.Benz
H
10

You can use this function:

Public Function Quarter(ByVal MonthNumber As Long) As Long

        If (MonthNumber < 1) Or (MonthNumber > 12) Then
                Call Err.Raise(6) ' Value out of Bounds '
        End If

        Let Quarter = (MonthNumber - 1) \ 3 + 1

End Function
Hellion answered 17/10, 2014 at 14:3 Comment(4)
Quarter = (Month(date()) + 2) \ 3 Slightly shorter. :)Ardoin
Nice one. I should go back to fractions basics. :-)Hellion
There was a MOD version that would roll whatever number you threw at it to be between 1 and 12. I can't find it and can't figure it out. :(Ardoin
Proper syntax now is Dim Quarter = (Month(Date.Now) + 2) \ 3Idola
I
1

In Excel VBA I like using Choose as sometimes I need to get the financial quarter rather than the calendar quarter.

calendar_quarter = Choose(Month(Date), 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)

' if financial year starts in April - this will get the financial quarter from the current date
financial_quarter = Choose(Month(Date), 4, 4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3)
Inae answered 2/2, 2022 at 18:48 Comment(0)
A
0

One liner for Excel:

=INT((MonthNo-1)/3 +1)

for SQL

floor(([MonthNo]-1)/3) +1
Akkad answered 8/7, 2019 at 11:22 Comment(0)
A
0

The format function will convert a date to the calendar quarter:

In string format: Quarter = Format((Date), "Q")

or use Val to convert to number format: Quarter = Val(Format((Date), "Q"))

Armipotent answered 13/3, 2024 at 23:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.