DateAdd
日付や時間の計算をする場合はDateAdd(interval, number, date)を用います。
intervalでは、年("yyyy")、月("m")、週("ww")、日("d")、時("h")、分("n")、秒("s")をセットします。
numberは、数値で設定し、未来はプラス、過去はマイナスです。
dateは、基準となる日付や時間です。
具体的な例は下記の通りです。
▽ 3か月後
'変数の設定
Dim ThisDate1, ThisDate2 As Date
'今日の日付・時間を取得
ThisDate1 = Now
'3か月後を取得
ThisDate2 = DateAdd("m", 3, ThisDate1)
'取得結果をメッセージボックスで出力
MsgBox "計算前 : " & Format(ThisDate1, "yyyy/MM/dd hh:mm:dd") & Chr(13) & _
"計算後 : " & Format(ThisDate2, "yyyy/MM/dd hh:mm:dd")
Dim ThisDate1, ThisDate2 As Date
'今日の日付・時間を取得
ThisDate1 = Now
'3か月後を取得
ThisDate2 = DateAdd("m", 3, ThisDate1)
'取得結果をメッセージボックスで出力
MsgBox "計算前 : " & Format(ThisDate1, "yyyy/MM/dd hh:mm:dd") & Chr(13) & _
"計算後 : " & Format(ThisDate2, "yyyy/MM/dd hh:mm:dd")
![](https://i0.wp.com/movie-memo.bookmarks.jp/blog/wp-content/uploads/2018/01/VBa028_02.png?resize=227%2C186&ssl=1)
▽ 3か月前
'変数の設定
Dim ThisDate1, ThisDate2 As Date
'今日の日付・時間を取得
ThisDate1 = Now
'3か月後を取得
ThisDate2 = DateAdd("m", -3, ThisDate1)
'取得結果をメッセージボックスで出力
MsgBox "計算前 : " & Format(ThisDate1, "yyyy/MM/dd hh:mm:dd") & Chr(13) & _
"計算後 : " & Format(ThisDate2, "yyyy/MM/dd hh:mm:dd")
Dim ThisDate1, ThisDate2 As Date
'今日の日付・時間を取得
ThisDate1 = Now
'3か月後を取得
ThisDate2 = DateAdd("m", -3, ThisDate1)
'取得結果をメッセージボックスで出力
MsgBox "計算前 : " & Format(ThisDate1, "yyyy/MM/dd hh:mm:dd") & Chr(13) & _
"計算後 : " & Format(ThisDate2, "yyyy/MM/dd hh:mm:dd")
![](https://i0.wp.com/movie-memo.bookmarks.jp/blog/wp-content/uploads/2018/01/VBa028_03.png?resize=227%2C186&ssl=1)