[英]Excel VBA: Macro function to calculate Revenue amoritization Schedule based on days
I am trying to write a macro function that will calculate revenue per month based on number of days in the month. 我正在尝试编写一个宏函数,该函数将根据当月的天数来计算每月的收入。
The trick is the start date and end date calculation, how to put that 诀窍是开始日期和结束日期的计算,如何计算
The inputs are 输入是
Number of days is calculated per month if deals starts in-between Total days revenue recognized is difference between Total days in start dates month and month total days. 如果交易在中间开始,则每月计算天数。已确认的总天数收入是指开始日期月份中的总天数与月份中的总天数之差。
Same with ending days for the contract if contract ends mid month 如果合同在月中旬结束,则与合同的结束日期相同
example 1 例子1
Thanks Brian 谢谢布莱恩
SAMPLE file with two tab, tab 1 has the example calculation. 带有两个选项卡的SAMPLE文件,选项卡1具有示例计算。 Tab 2 has the data I get and the months are the amount I calculate and want to use a macro to automate Link to Sample excel file
选项卡2包含我获取的数据,月份是我计算的数量,并且希望使用宏来自动化链接到Sample excel文件的链接
This is a non VBA solution considering the following 这是非VBA解决方案,请考虑以下因素
and the formula for to be entered in G2 and copied from G2 to CX... is 并且要在G2中输入并从G2复制到CX ...的公式是
=IF(OR($B2>EOMONTH(G$1,0),$C2<G$1-DAY(G$1)+1),0,IF($C2>EOMONTH(G$1,0),EOMONTH(G$1,0),$C2)-IF($B2>G$1-DAY(G$1)+1,$B2,G$1-DAY(G$1)+1)+1)*$E2
And the VBA solution (though not suggested) is VBA解决方案 (尽管不建议)是
Sub doCalc()
Dim TCVRng As Range, SdtRng As Range, FdtRng As Range, TermLenRng As Range, MonRng As Range
Dim i As Long
'Modify ranges according to your requirement
Set MonRng = ActiveSheet.Range("G1:CX1")
For i = 2 To 8
Set TCVRng = ActiveSheet.Cells(i, 1)
Set SdtRng = ActiveSheet.Cells(i, 2)
Set FdtRng = ActiveSheet.Cells(i, 3)
Set TermLenRng = ActiveSheet.Cells(i, 4)
'Debug.Print TCVRng.Value, SdtRng.Value, FdtRng.Value
'Debug.Print "============================================="
'to bypass any intermidate summaty rowrow
' If TCVRng.Value > 0 And IsDate(SdtRng.Value) And IsDate(FdtRng.Value) Then
MonthCal TCVRng, SdtRng, FdtRng, TermLenRng, MonRng
' End If
Next
End Sub
Private Sub MonthCal(TCVRng As Range, SdtRng As Range, FdtRng As Range, TermLenRng As Range, MonRng As Range)
Dim TCV As Single, Sdt As Date, Fdt As Date, TermLen As Single, PerDay As Single
Dim Msdt As Date, Medt As Date, MnAmnt As Single, MnDay As Integer
Dim Cel As Range, Col As Long, ofst As Long
TCV = TCVRng.Value
Sdt = SdtRng.Value
Fdt = FdtRng.Value
TermLen = TermLenRng.Value
PerDay = (TCV / 365) / (TermLen / 12)
For Each Cel In MonRng
ofst = Cel.Column - TCVRng.Column
Msdt = Cel.Value
Msdt = DateAdd("d", -Day(Msdt) + 1, Msdt)
Medt = DateAdd("m", 1, Msdt)
Medt = DateAdd("d", -1, Medt)
MnDay = IIf(Sdt > Medt Or Fdt < Msdt, 0, IIf(Fdt < Medt, Fdt, Medt) - IIf(Sdt > Msdt, Sdt, Msdt) + 1)
MnAmnt = MnDay * PerDay
'Debug.Print TCV, Sdt, Fdt, Msdt, Medt, MnDay, MnAmnt
TCVRng.Offset(, ofst).Value = MnAmnt
Next Cel
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.