【问题标题】:Access VBA Get Quarter From Month Number访问 VBA 从月份数获取季度
【发布时间】:2014-12-13 03:11:53
【问题描述】:

我一直在尝试从月份编号中查找季度编号,即我有月份编号 2,它应该是季度编号 1,我如何在 Access VBA 或访问查询中执行此操作? 提前致谢。

【问题讨论】:

  • 你可以使用 Choose(MonthNum,1,1,1,2,2,2,3,3,3,4,4,4)
  • @Dave 你应该把它作为一个答案。它唯一缺少的是错误/越界处理。

标签: vba date ms-access


【解决方案1】:

你可以使用这个功能:

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

【讨论】:

  • Quarter = (Month(date()) + 2) \ 3 略短。 :)
  • 不错的一个。我应该回到分数基础。 :-)
  • 有一个 MOD 版本,可以将你扔给它的任何数字滚动到 1 到 12 之间。我找不到它,也想不出来。 :(
  • 正确的语法现在是Dim Quarter = (Month(Date.Now) + 2) \ 3
【解决方案2】:

Excel 的一个衬里:

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

对于 SQL

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

【讨论】:

    【解决方案3】:

    在 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)
    

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 2020-11-16
      • 2023-03-14
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多