Sub GetInvoiceTerm()
' チェック
If False = IsDate(UserForm1.TextBox49.Text) Then Exit Sub
If False = IsNumeric(UserForm1.TextBox50.Text) Then Exit Sub
If False = IsNumeric(UserForm1.TextBox51.Text) Then Exit Sub
current_date = CDate(UserForm1.TextBox49.Text) ' 今回の締日
master_closing_date = CInt(UserForm1.TextBox50.Text) ' 得意先締日
payment_duration = CInt(UserForm1.TextBox51.Text) ' 支払予定(経過日数)
start_day = ""
end_day = ""
If master_closing_date <> "30" Then
' 末締以外
If CInt(Day(current_date)) > CInt(master_closing_date) Then
tmp = DateAdd("m", 1, current_date)
start_day = Year(current_date) & "/" & Month(current_date) & "/" & master_closing_date + 1
end_day = Year(tmp) & "/" & Month(tmp) & "/" & master_closing_date
Else
tmp = DateAdd("m", -1, current_date)
start_day = Year(tmp) & "/" & Month(tmp) & "/" & master_closing_date + 1
end_day = Year(current_date) & "/" & Month(current_date) & "/" & master_closing_date
End If
Else
' 末締の場合
start_day = Year(current_date) & "/" & Month(current_date) & "/1"
end_day = Year(current_date) & "/" & Month(current_date) & "/" & GetLastDay(current_date)
End If
If False = IsDate(start_day) Or False = IsDate(end_day) Then
' 得意先締日の値からstart_day、end_dayが有効な日付にならなかった場合
UserForm1.TextBox41.Text = ""
UserForm1.TextBox46.Text = ""
UserForm1.TextBox44.Text = ""
Exit Sub
End If
UserForm1.TextBox41.Text = Format(CDate(start_day), "yyyy/mm/dd")
UserForm1.TextBox46.Text = Format(CDate(end_day), "yyyy/mm/dd")
' 支払期限算出--------------------------------------------------------
' 支払予定(経過日数)を30で割り×月、余り×日
m = payment_duration / 30
d = payment_duration Mod 30
If True = IsLastDay(CDate(end_day)) Then
' end_dayが月末の場合は指定した月数を加算し月末にする。
tmp1 = DateAdd("m", m, CDate(end_day))
tmp1 = CDate(Year(tmp1) & "/" & Month(tmp1) & "/" & GetLastDay(tmp1))
tmp2 = DateAdd("d", d, tmp1)
Else
' end_dayが月末以外
tmp1 = DateAdd("m", m, CDate(end_day))
tmp2 = DateAdd("d", d, tmp1)
End If
UserForm1.TextBox44.Text = Format(tmp2, "yyyy/mm/dd")
End Sub
Function IsLastDay(target) ' 渡された日付が月末かどうか
' 呼出側で日付型か確認済
If Day(target) = GetLastDay(target) Then
IsLastDay = True
Else
IsLastDay = False
End If
End Function
Function GetLastDay(target) ' 渡された日付の月の月末を取得
' 呼出側で日付型か確認済
GetLastDay = Day(DateAdd("d", -1, Year(DateAdd("m", 1, target)) & "/" & Month(DateAdd("m", 1, target)) & "/1"))
End Function