根據(jù)每月費(fèi)用標(biāo)準(zhǔn)、費(fèi)用起止日期計(jì)算期間費(fèi)用金額
注意:
- 每月費(fèi)用標(biāo)準(zhǔn)為一個(gè)定值茴厉,不論該月天數(shù)
- 整月的判斷泽台,日期滿足(月+n、日-1)即為整月矾缓,舉例:2022.2.2與2022.3.1期間為1個(gè)整月
- 所有計(jì)算保留2位小數(shù)
Function datecost(startdate As Date, enddate As Date, monthcost As Double) As Double
'函數(shù)定義datacost(開(kāi)始日期怀酷,結(jié)束日期,每月費(fèi)用標(biāo)準(zhǔn))嗜闻,計(jì)算該周期內(nèi)一共的費(fèi)用
month_count = DateDiff("m", startdate, enddate) '月差數(shù)蜕依,整月計(jì)算
m = DateSerial(Year(startdate), Month(startdate) + month_count, Day(startdate) - 1) '月差數(shù)整月后的日期
date1 = DateSerial(Year(startdate), Month(startdate) + 1, 0) '開(kāi)始日期當(dāng)月的結(jié)束日期,計(jì)算當(dāng)月天數(shù)
date2 = DateSerial(Year(enddate), Month(enddate) + 1, 0) '結(jié)束日期當(dāng)月的結(jié)束日期琉雳,計(jì)算當(dāng)月天數(shù)
date3 = DateSerial(Year(enddate), Month(enddate), 1) '結(jié)束日期當(dāng)月的開(kāi)始日期
date_count1 = DateDiff("d", startdate, date1) + 1 '開(kāi)始日期距當(dāng)月末天數(shù)样眠,含當(dāng)日,開(kāi)始月費(fèi)用天數(shù)
date_count2 = DateDiff("d", date3, enddate) + 1 '結(jié)束日期距當(dāng)月初天數(shù)咐吼,含當(dāng)日吹缔,結(jié)束月費(fèi)用天數(shù)
date_count3 = DateDiff("d", startdate, enddate) + 1 '開(kāi)始日期距結(jié)束日期天數(shù),含當(dāng)日锯茄,同一月內(nèi)費(fèi)用天數(shù)
If DateDiff("d", enddate, m) = 0 Then '日期相等判斷厢塘,是否為整月,閏月不影響
datecost = monthcost * month_count
ElseIf DateDiff("d", date1, date2) = 0 Then '開(kāi)始日期肌幽,結(jié)束日期在同一個(gè)月晚碾,非整月
datecost = Round(date_count3 / Day(date1) * monthcost, 2) '同一個(gè)月當(dāng)月費(fèi)用天數(shù)所產(chǎn)生的的費(fèi)用
Else '開(kāi)始日期,結(jié)束日期不在同一個(gè)月喂急,構(gòu)成期間非整月
date_cost1 = Round(date_count1 / Day(date1) * monthcost, 2) '開(kāi)始日期當(dāng)月費(fèi)用天數(shù)所產(chǎn)生的的費(fèi)用
date_cost2 = Round(date_count2 / Day(date2) * monthcost, 2) '結(jié)束日期當(dāng)月費(fèi)用天數(shù)所產(chǎn)生的的費(fèi)用
month_count = DateDiff("m", date1, date2) - 1 '開(kāi)始日期格嘁,結(jié)束日期中間間隔整月數(shù)量
month_cost = month_count * monthcost
datecost = date_cost1 + date_cost2 + month_cost
End If
End Function
Sub datecost幫助信息()
'運(yùn)行一次后該幫助信息生效
Dim 函數(shù)名稱 As String '函數(shù)名稱
Dim 函數(shù)描述 As String '函數(shù)描述
Dim 參數(shù)個(gè)數(shù)(3) As String '函數(shù)參數(shù)描述 數(shù)組 個(gè)數(shù)
函數(shù)名稱 = "datecost"
函數(shù)描述 = "根據(jù)每月費(fèi)用標(biāo)準(zhǔn)、費(fèi)用起止日期計(jì)算期間費(fèi)用金額"
參數(shù)個(gè)數(shù)(0) = "參數(shù)1:費(fèi)用開(kāi)始日期廊移,日期格式"
參數(shù)個(gè)數(shù)(1) = "參數(shù)2:費(fèi)用結(jié)束日期糕簿,日期格式"
參數(shù)個(gè)數(shù)(2) = "參數(shù)3:每月費(fèi)用標(biāo)準(zhǔn)金額探入,數(shù)字格式"
Call Application.MacroOptions(macro:=函數(shù)名稱, Description:=函數(shù)描述, ArgumentDescriptions:=參數(shù)個(gè)數(shù))
End Sub