Excel·VBA自定义函数按季度分摊费用

根据待分摊费用总额、费用起止日期、待分摊归属季度,计算该季度应分摊费用。

调用《Excel·VBA自定义函数计算每月费用标准》计算每月费用标准,再调用《Excel·VBA自定义函数计算日期期间费用》计算该季度应分摊费用

注意:因保留2位小数,经monthcost、datecost计算后,分摊费用的合计数与原待分摊费用存在一定差额
Function costshare(cost1 As Double, startdate1 As Date, enddate1 As Date, quarter1 As Integer) As Double
    '函数定义costshare(待分摊费用,费用开始日期,费用结束日期,季度数),计算该季度应分摊的费用
    '季度数:取值范围 [1,++] ,计数从开始日期当年的1季度起,即 1 为开始日期当年1季度,表示1季度应分摊费用,以此类推
    
    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
    '以上为定义参数变量和赋值,否则无法调用其他模块定义的函数
    
    month_cost = monthcost.monthcost(cost, startdate, enddate)   '计算每月标准费用,调用其他模块的函数,句点法
    date_quarter1 = DateSerial(Year(startdate), quarter * 3 - 2, 1)   '待分摊季度初的日期
    date_quarter2 = DateSerial(Year(startdate), quarter * 3 + 1, 0)   '待分摊季度末的日期
    
    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
        '起止日期都在待输出季度初末日期内
        costshare = cost
    ElseIf DateDiff("d", date_quarter1, startdate) >= 0 And DateDiff("d", date_quarter2, enddate) >= 0 Then
        '待输出季度仅季末日期在起止日期内
        costshare = datecost.datecost(startdate, date_quarter2, month_cost)
    ElseIf DateDiff("d", startdate, date_quarter1) >= 0 And DateDiff("d", date_quarter2, enddate) >= 0 Then
        '待输出季度初末都在起止日期内
        costshare = month_cost * 3
    ElseIf DateDiff("d", startdate, date_quarter1) >= 0 And DateDiff("d", enddate, date_quarter2) >= 0 Then
        '待输出季度仅季初在起止日期内
        costshare = datecost.datecost(date_quarter1, enddate, month_cost)
    End If
    
End Function

Sub costshare帮助信息()
    '运行一次后该帮助信息生效
    
    Dim 函数名称 As String        '函数名称
    Dim 函数描述 As String        '函数描述
    Dim 参数个数(4) As String     '函数参数描述 数组 个数
    
    函数名称 = "costshare"
    函数描述 = "根据待分摊费用总额、费用起止日期、待分摊归属季度,计算该季度应分摊费用"
    参数个数(0) = "参数1:待分摊费用,数字格式"
    参数个数(1) = "参数2:费用开始日期,日期格式"
    参数个数(2) = "参数3:费用结束日期,日期格式"
    参数个数(3) = "参数4:待分摊归属季度,自开始日期当年度1季度开始计数,数字格式"
    
    Call Application.MacroOptions(macro:=函数名称, Description:=函数描述, ArgumentDescriptions:=参数个数)
    
End Sub
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容