2010/06/11

VBA 日付関数草案

'対象日に対応する締め日
'対象日:ExpressionDate:Date
'締日:CutoffDayNum:Int
'戻り値:直近の締め日:Date
Function CutoffDate(ExpressionDate As Date, ByVal CutoffDayNum As Integer) As Date
Dim tmp As String
    tmp = Format(DateAdd("m", 1, ExpressionDate - CutoffDayNum), "yyyy/mm/")
    Do Until IsDate(tmp & CStr(CutoffDayNum))
        CutoffDayNum = CutoffDayNum - 1
    Loop
    CutoffDate = DateValue(tmp & CStr(CutoffDayNum))
End Function

'締め日に対応する支払日
'締め日:ExpressionDate:Date
'隔月:IntervalMonth:Int 0:当月 1:翌月 2:翌々 3:翌々々 ....
'月支払日:DayNum:Int
'戻り値:IntervalMonth指定の支払日:Date

Function PayDate(ExpressionDate As Date, IntervalMonth As Integer, ByVal DayNum As Integer) As Date
Dim tmp As String
    tmp = Format(DateAdd("m", IntervalMonth, ExpressionDate), "yyyy/mm/")
    Do Until IsDate(tmp & CStr(DayNum))
        DayNum = DayNum - 1
    Loop
    PayDate = DateValue(tmp & CStr(DayNum))
End Function

'対象日に対応する月初日
'対象日:ExpressionDate:Date
'隔月数IntervalMonth:Int 0:当月 1:翌月 -1:前月
'戻り値:IntervalMonth指定の月初日:Date
Function StartOfMonth(ExpressionDate As Date, Optional IntervalMonth As Integer = 0)
    StartOfMonth = DateSerial(Year(ExpressionDate), Month(ExpressionDate) + IntervalMonth, 1)
End Function

'対象日に対応する月末日
'対象日:ExpressionDate:Date
'隔月数IntervalMonth:Int 0:当月 1:翌月 -1:前月
'戻り値:IntervalMonth指定の月末日:Date
Function EndOfMonth(ExpressionDate As Date, Optional IntervalMonth As Integer = 0)
    EndOfMonth = DateSerial(Year(ExpressionDate), Month(ExpressionDate) + IntervalMonth + 1, 0)
End Function

0 件のコメント: