根據(jù)待分?jǐn)傎M(fèi)用總額纵诞、費(fèi)用起止日期搭独、待分?jǐn)倸w屬季度悼凑,計(jì)算該季度應(yīng)分?jǐn)傎M(fèi)用晦炊。
調(diào)用《Excel·VBA自定義函數(shù)計(jì)算每月費(fèi)用標(biāo)準(zhǔn)》計(jì)算每月費(fèi)用標(biāo)準(zhǔn),再調(diào)用《Excel·VBA自定義函數(shù)計(jì)算日期期間費(fèi)用》計(jì)算該季度應(yīng)分?jǐn)傎M(fèi)用
注意:因保留2位小數(shù)嗅绸,經(jīng)monthcost脾猛、datecost計(jì)算后,分?jǐn)傎M(fèi)用的合計(jì)數(shù)與原待分?jǐn)傎M(fèi)用存在一定差額
Function costshare(cost1 As Double, startdate1 As Date, enddate1 As Date, quarter1 As Integer) As Double
'函數(shù)定義costshare(待分?jǐn)傎M(fèi)用鱼鸠,費(fèi)用開始日期猛拴,費(fèi)用結(jié)束日期,季度數(shù))蚀狰,計(jì)算該季度應(yīng)分?jǐn)偟馁M(fèi)用
'季度數(shù):取值范圍 [1,++] 愉昆,計(jì)數(shù)從開始日期當(dāng)年的1季度起,即 1 為開始日期當(dāng)年1季度麻蹋,表示1季度應(yīng)分?jǐn)傎M(fèi)用跛溉,以此類推
Dim cost As Double, startdate As Date, enddate As Date, quarter As Integer
Dim month_cost As Double, date_quarter1 As Date, date_quarter2 As Date
cost = cost1
startdate = startdate1
enddate = enddate1
quarter = quarter1
'以上為定義參數(shù)變量和賦值,否則無法調(diào)用其他模塊定義的函數(shù)
month_cost = monthcost.monthcost(cost, startdate, enddate) '計(jì)算每月標(biāo)準(zhǔn)費(fèi)用扮授,調(diào)用其他模塊的函數(shù)芳室,句點(diǎn)法
date_quarter1 = DateSerial(Year(startdate), quarter * 3 - 2, 1) '待分?jǐn)偧径瘸醯娜掌? date_quarter2 = DateSerial(Year(startdate), quarter * 3 + 1, 0) '待分?jǐn)偧径饶┑娜掌?
If DateDiff("d", enddate, date_quarter1) > 0 Or DateDiff("d", date_quarter2, startdate) > 0 Then
'起止日期與待輸出季度初末日期沒有交集
costshare = 0
ElseIf DateDiff("d", date_quarter1, startdate) >= 0 And DateDiff("d", enddate, date_quarter2) >= 0 Then
'起止日期都在待輸出季度初末日期內(nèi)
costshare = cost
ElseIf DateDiff("d", date_quarter1, startdate) >= 0 And DateDiff("d", date_quarter2, enddate) >= 0 Then
'待輸出季度僅季末日期在起止日期內(nèi)
costshare = datecost.datecost(startdate, date_quarter2, month_cost)
ElseIf DateDiff("d", startdate, date_quarter1) >= 0 And DateDiff("d", date_quarter2, enddate) >= 0 Then
'待輸出季度初末都在起止日期內(nèi)
costshare = month_cost * 3
ElseIf DateDiff("d", startdate, date_quarter1) >= 0 And DateDiff("d", enddate, date_quarter2) >= 0 Then
'待輸出季度僅季初在起止日期內(nèi)
costshare = datecost.datecost(date_quarter1, enddate, month_cost)
End If
End Function
Sub costshare幫助信息()
'運(yùn)行一次后該幫助信息生效
Dim 函數(shù)名稱 As String '函數(shù)名稱
Dim 函數(shù)描述 As String '函數(shù)描述
Dim 參數(shù)個(gè)數(shù)(4) As String '函數(shù)參數(shù)描述 數(shù)組 個(gè)數(shù)
函數(shù)名稱 = "costshare"
函數(shù)描述 = "根據(jù)待分?jǐn)傎M(fèi)用總額、費(fèi)用起止日期刹勃、待分?jǐn)倸w屬季度堪侯,計(jì)算該季度應(yīng)分?jǐn)傎M(fèi)用"
參數(shù)個(gè)數(shù)(0) = "參數(shù)1:待分?jǐn)傎M(fèi)用,數(shù)字格式"
參數(shù)個(gè)數(shù)(1) = "參數(shù)2:費(fèi)用開始日期荔仁,日期格式"
參數(shù)個(gè)數(shù)(2) = "參數(shù)3:費(fèi)用結(jié)束日期伍宦,日期格式"
參數(shù)個(gè)數(shù)(3) = "參數(shù)4:待分?jǐn)倸w屬季度,自開始日期當(dāng)年度1季度開始計(jì)數(shù)乏梁,數(shù)字格式"
Call Application.MacroOptions(macro:=函數(shù)名稱, Description:=函數(shù)描述, ArgumentDescriptions:=參數(shù)個(gè)數(shù))
End Sub