LoginSignup
1
1

More than 3 years have passed since last update.

【VBA】日付から月末を求める方法

Posted at

今回は、月末を求める方法です。

サンプルデータは以下の図をご覧ください。

サンプル

↓実演動画
https://youtu.be/PKS9JwrsPG0

ソースコードは下記の通りです。


Sub 月末を求める()

  Cells(2, 2) = DateSerial(Year(Cells(2, 1)), Month(Cells(2, 1)) + 1, 0)

End Sub

詳細を説明します。

DateSerial(Year(Cells(2, 1)), Month(Cells(2, 1)) + 1, 0)

今回は、DateSerial関数を使用します。
DateSerial関数は、指定した日付を返す関数になります。

第1引数、第2引数、第3引数には、それぞれ年、月、日を指定します。

DateSerial関数の詳細はこちら
https://www.tipsfound.com/vba/05dateserial

値に0を入れると、一つ前の値が返されます。
※例えば、元データが2021/4/15だった場合、日を0とすると2021/3/31が返される。

今回は、今月末を取得するため日を0にし、前月末が取得されるため月に+1することで期待する値が取得出来ました。

以上が、月末を求める方法でした。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1