宮崎県延岡市 | スマイルカラーソフトウェア | ユーザースタイルに合わせたプログラミングでお客様に幸せをお届けします。

任意の年月の月初日、月末日を取得

月の初日は1日ですが、月末日は年月により変わりますね。 年・月・日で日付を返す DateSerial 関数を使ってそれぞれの日付を返すプロシージャです。

Public Function EndOfTheMonth(ByVal wDate As Date, ByVal Index As Integer) As Date
' 指定された日付の月の前後月の最後の日付を返します
' Index=-1: 前月末、Index=0: 当月末、Index=1: 翌月末
'
    EndOfTheMonth = DateSerial(Year(wDate), Month(wDate) + Index + 1, 0)
End Function

Public Function FirstOfTheMonth(ByVal wDate As Date, ByVal Index As Integer) As Date
' 指定された日付の月の前後月の最初の日付を返します
' Index=-1: 前月初、Index=0: 当月初、Index=1: 翌月初
'
    FirstOfTheMonth = DateSerial(Year(wDate), Month(wDate) + Index, 1)
End Function

以下のように使用します。

MsgBox "来月1日は、" & Format(FirstOfTheMonth(Date, 1), "yyyy年m月d日") & "です。" & _
       vbCrLf & "前月の月末は、" & Format(EndOfTheMonth(Date, -1), "yyyy年m月d日") & "です。"