【问题标题】:VBA: Rename sheet to previous month, yearVBA:将工作表重命名为上个月,年份
【发布时间】:2021-07-15 17:16:54
【问题描述】:

我想将工作表重命名为上个月,年份 - “MMM yyyy” 例如,使用今天的日期,我希望将工作表重命名为“JUN 2021”

我有以下代码,但这一直输出“1905 年 1 月”。这也不包括今天的月份是 2022 年 1 月的情况。使用此代码的输出将是“2022 年 12 月”

Sub test()
    Sheets("Sheet1").Name = Format(Month(Date) - 1, "MMM") & Format(Year(Date), "YYYY")
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以使用EoMonth。您还需要将格式化输出大写:MMM 实际上并没有大写月份。

    Dim lastMonth As Date
    lastMonth = WorksheetFunction.EoMonth(Date, -1)
    
    Sheets("Sheet1").Name = UCase$(Format$(lastMonth, "Mmm Yyyy")
    

    【讨论】:

      【解决方案2】:

      试试这个:

      Sheets("Sheet1").Name = Format(DateValue(Month(Date) - 1 & "/1/2021"), "mmm yyyy")
      

      【讨论】:

        猜你喜欢
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-07
        • 1970-01-01
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        相关资源
        最近更新 更多