【问题标题】:Excel VBA: Date ComparisonExcel VBA:日期比较
【发布时间】:2017-07-18 13:42:05
【问题描述】:

所以我目前正在尝试编写一个代码来将当前日期与其他两个日期进行比较,以确定信息的有效性。例如,如果日期介于一年的第一季度和第二季度之间,则文档上的信息截至第一季度日期(3 月 31 日)。以下是我目前拥有的内容,出于某种原因,即使当前日期是 7 月,代码仍然显示该信息截至 3 月 31 日有效。有人有什么建议吗?

crntDate = Date
q1End = CDate("Mar 31" & " " & Year(Date))
q2End = CDate("Jun 30" & " " & Year(Date))
q3End = CDate("Sep 30" & " " & Year(Date))
q4End = CDate("Dec 31" & " " & Year(Date))

If q1End <= crntDate <= q2End Then
    quart = "Q1" & " " & Year(Date)
ElseIf q2End <= crntDate <= q3End Then
    quart = "Q2" & " " & Year(Date)
ElseIf q3End <= crntDate <= q4End Then
    quart = "Q3" & " " & Year(Date)
Else
    quart = "Q4" & " " & Year(Date)
End If

shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
With wdApp.ActiveDocument
    .SaveAs2 "https://path/" & shName & ".docx"
    .Close
End With

【问题讨论】:

    标签: vba excel date excel-2010


    【解决方案1】:

    如果您尝试将日期格式化为季度,则不需要所有结束日期和比较,您可以在 VBA 中使用整数除法 \

    Sub test()
    
      Dim quart As String
      quart = GetDateAsQuarterYear(VBA.Date)
    
      shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
      With wdApp.ActiveDocument
        .SaveAs2 "https://path/" & shName & ".docx"
        .Close
      End With
    End Sub
    
    Function GetDateAsQuarterYear(crntDate As Date) As String
    
      Dim quarterEnd As Date
      quarterEnd = DateSerial(Year(crntDate), 1 + 3 * (1 + (Month(crntDate) - 1) \ 3), 0)
    
      GetDateAsQuarterYear = "Q" & 1 + (Month(crntDate) - 1) \ 3 & " (" & Format$(quarterEnd, "mmmm d, yyyy") & ")"
    
    End Function
    

    【讨论】:

    • 如果我正在寻找与该函数一起出现的实际季度日期怎么办? (即 3 月 31 日,第一季度的年份,6 月 30 日,第二季度的年份等)
    • 我已更新代码以修复非季末月份的错误,并以Q3 (September 30, 2017) 之类的格式添加季末日期
    【解决方案2】:

    q1End &lt;= crntDate &lt;= q2End 在 Excel 中不起作用,它需要:

    q1End <= crntDate  and crntDate <= q2End
    

    所以

    crntDate = Date
    q1End = CDate("Mar 31" & " " & Year(Date))
    q2End = CDate("Jun 30" & " " & Year(Date))
    q3End = CDate("Sep 30" & " " & Year(Date))
    q4End = CDate("Dec 31" & " " & Year(Date))
    
    If q1End <= crntDate  and crntDate <= q2End Then
        quart = "Q2" & " " & Year(Date)
    ElseIf q2End <= crntDate  and crntDate <= q3End Then
        quart = "Q3" & " " & Year(Date)
    ElseIf q3End <= crntDate  and crntDate <= q4End Then
        quart = "Q4" & " " & Year(Date)
    Else
        quart = "Q1" & " " & Year(Date)
    End If
    
    shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
    With wdApp.ActiveDocument
        .SaveAs2 "https://path/" & shName & ".docx"
        .Close
    End With
    

    【讨论】:

    • 该死的,刚要发帖:)
    • 仍然在想我在用 Python 编程 - 谢谢@ScottCraner :)
    • IIUC,, CDate 将受制于用户的区域/语言设置,因此如果您在 VBA 中声明日期,最好使用 DateSerial(year,month,day) 或本机语法#mm/dd/yyyy#.
    • 如果代码在新年前夜的午夜运行也很不幸,因为重复调用Year(Date) 可能会以不同的年份结束。
    • 您错过了仅对四分之一使用整数除法的优雅解决方案;-p
    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多