
【VBA】エクセルで年度を計算するやりかた
マクロを組んだことがない、という方はこちらを参考にしてはじめの設定をしてください。
次のコードで日付の年度を計算する関数を作成できる。
Private Function nendo(ByVal inputDate As Date) As String
Dim threeMonthsBefore As Date
' 日付の3ヶ月前を計算
threeMonthsBefore = DateAdd("m", -3, inputDate)
' 3ヶ月前の日付の年を取得
' nendo = Year(threeMonthsBefore) & "年度"
nendo = Year(threeMonthsBefore)
End Function
関数で隣のセルの年度を表示させることができる。

Enterを押下すると

■解説
Function ***(***) As ***で関数を定義することができる。オリジナルの関数を作成できるということ。Private Function とすることで、このモジュールのみ、このエクセル内のみで使用可能な関数とした。年度=3カ月前は何年だったか、というコンセプトは、こちらと同じだ。