1

Excel スプレッドシートから日付を取得し、1 か月を減算して MMM-YY に再フォーマットすることを目的とした Excel マクロ (VBA) に問題があります。基本的に、2013 年 3 月 31 日を 2 月 13 日に変換したい

これが私のコードです:

Dim ReportDate As Date
ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm") & "-" & Format(ReportDate, "yy")
Debug.Print prevMonth

私が得る結果は2/31/2013-13です

そこで、prevMonth 変数を変更してみました。

prevMonth = Format((Month(ReportDate) - 1) & "/" & Day(ReportDate) & "/" & Year(ReportDate), "mmm-yy")

しかし、2013 年 2 月 31 日に再び

prevMonth を Integer または Date として宣言しようとしましたが、型の不一致エラーが発生します。String として宣言することしかできませんが、それでもプログラムには役立ちません。

よろしくお願いします。

4

1 に答える 1

6

これを試して

Sub Demo()
    Dim ReportDate As Date
    Dim prevMonth As Date

    ReportDate = Worksheets("Current").Cells(2, 16) 'ex. 03-31-2013
    prevMonth = DateAdd("m", -1, ReportDate)
    Debug.Print Format(prevMonth, "mmm-yy")
End Sub
于 2013-04-05T20:41:59.147 に答える