月末日の取得@Excel マクロ・VBA
月末の取得を行います。
例えば、今月の末日を取得したい場合は、
DateValueにて、今月の1日を作成し、DateAddにより月を+1します。
それに対して、DateAddにより、日を-1します。
取得した月の来月の1日目に対して、日を-1すればいいわけです。
Dim tempData As Date
'今月末日
nowDate = DateTime.Now
lastMonth = DateAdd("m", 1, DateValue(CStr(Year(nowDate)) & "/" & CStr(Month(nowDate)) & "/1"))
lastMonth = DateAdd("d", -1, lastMonth)
MsgBox lastMonth
'2008/12/5の末日
nowDate = "2008/12/5"
lastMonth = DateAdd("m", 1, DateValue(CStr(Year(nowDate)) & "/" & CStr(Month(nowDate)) & "/1"))
lastMonth = DateAdd("d", -1, lastMonth)
MsgBox lastMonth
|
|