访问 VBA 从月份编号获取季度

问题描述 投票:0回答:4

我一直试图从月份编号中查找季度编号,即我的月份编号 2 应该是季度编号 1,我该如何在 Access VBA 或访问查询中执行此操作? 预先感谢。

vba date ms-access
4个回答
10
投票

您可以使用此功能:

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

1
投票

在 Excel VBA 中,我喜欢使用“选择”,因为有时我需要获取财务季度而不是日历季度。

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)

0
投票

Excel 的一个衬垫:

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

对于 SQL

floor(([MonthNo]-1)/3) +1

0
投票

格式函数会将日期转换为日历季度:

字符串格式: 季度 = 格式((日期), "Q")

或者使用 Val 转换为数字格式: 季度 = Val(格式((日期), "Q"))

© www.soinside.com 2019 - 2024. All rights reserved.