ExcelのVBA、マクロで、月の日数を取得するFunctionは標準では用意されていません(ノд・。)今回は、日数取得の考え方とその方法をご紹介致します(。・ω・。)
1. Excel VBAで月の日数を取得する際の考え方
VBAで日数を取得する際は、下記の考え方で取得します(。・ω・。)
月末日を取得して、その日付部分のみを取得する
2. Sample Code
'# 当月の月末月初取得 Debug.Print DateSerial(Year(Date), Month(Date), 1) '# 月初 Debug.Print DateSerial(Year(Date), Month(Date) + 1, 0) '# 月末 '# 先月の月末月初取得 Debug.Print DateSerial(Year(Date), Month(Date) - 1, 1) '# 月初 Debug.Print DateSerial(Year(Date), Month(Date), 0) '# 月末 '# 来月の月末月初取得 Debug.Print DateSerial(Year(Date), Month(Date) + 1, 1) '# 月初 Debug.Print DateSerial(Year(Date), Month(Date) + 2, 0) '# 月末
3. コード解説
Debug.Printは、デバックする際に利用するもので、VBEの画面にログが出力されます。アプリケーション側で確認する際は、MsgBoxにして下さい(。・ω・。)
DateSerial関数で、年・月・日の数値から日付を計算します。日=0は、前月末日となります(。・ω・。)
Windows関連記事
Windows関連のその他記事もまとめてますので、是非お立ち寄りください(。・ω・。)
この記事へのコメントはありません。