#2
阳光上的桥2023-06-20 14:25
试试看这次是不是你需要的(最后一个不满季/半年/年的租金收取方式有多种理解,程序选择的是满期的次月收取,也可以修改为前一季度收取):
程序代码: Option Explicit '月份序号:公元0年元旦至今的月份序号 Private Sub test() Dim startDate As Date, endDate As Date, d As Date, period$, y%, m%, ym& startDate = #5/10/2020# endDate = #5/9/2022# period = "季" Debug.Print startDate, endDate, period For ym = Date2YM(startDate) To Date2YM(endDate) + 3 d = YM2Date(ym) y = Year(d) m = Month(d) Debug.Print d, CalculateRent(startDate, endDate, period, 100, y, m) Next ym End Sub Function CalculateRent(startDate, endDate, period, rentAmt, curYear, curMonth) As Double Dim preDate1 As Date, preDate2 As Date, renDate1 As Date, renDate2 As Date Dim m&, ym&, n&, i&, j&, k& Select Case Left(Trim(period), 1) Case "月": m = 1 '结算周期月份数 Case "季": m = 3 Case "半": m = 6 Case "年": m = 12 End Select i = Date2YM(startDate) '起租月 n = Date2YM(endDate) '止租月 ym = curYear * 12 + curMonth '当前月 k = WorksheetFunction.RoundUp((ym - i) / m, 0) '当前收租周期序号,0... j = (ym - i) Mod m '本周期内已过月份数 preDate1 = YM2Date(i + (k - 1) * m) '本租期开始日 preDate2 = YM2Date(i + k * m) - 1 '本租期结束日 renDate1 = IIf(preDate1 > startDate, preDate1, startDate) '去除起租日前的 renDate2 = IIf(preDate2 > endDate, endDate, preDate2) '去除终止日后的 If (ym > i) And (ym < n + m) And j = 0 Then '租期内 CalculateRent = rentAmt * (renDate2 - renDate1) / (preDate2 - preDate1) ElseIf ym = n + 1 Then '满期的次月 CalculateRent = rentAmt * (renDate2 - renDate1) / (preDate2 - preDate1) End If End Function Private Function Date2YM(d) '日期转换为月份序号 Date2YM = Year(d) * 12 + Month(d) End Function Private Function YM2Date(ym) '月份序号转日期 YM2Date = DateSerial(ym \ 12, ym Mod 12, 1) End Function 测试情况 程序代码: 2020/5/10 2022/5/9 月 2020/5/1 0 2020/6/1 70 2020/7/1 100 2020/8/1 100 2020/9/1 100 2020/10/1 100 2020/11/1 100 2020/12/1 100 2021/1/1 100 2021/2/1 100 2021/3/1 100 2021/4/1 100 2021/5/1 100 2021/6/1 100 2021/7/1 100 2021/8/1 100 2021/9/1 100 2021/10/1 100 2021/11/1 100 2021/12/1 100 2022/1/1 100 2022/2/1 100 2022/3/1 100 2022/4/1 100 2022/5/1 100 2022/6/1 26.6666666666667 2022/7/1 0 2022/8/1 0 2020/5/10 2022/5/9 季 2020/5/1 0 2020/6/1 90.1098901098901 2020/7/1 90.1098901098901 2020/8/1 90.1098901098901 2020/9/1 100 2020/10/1 100 2020/11/1 100 2020/12/1 100 2021/1/1 100 2021/2/1 100 2021/3/1 100 2021/4/1 100 2021/5/1 100 2021/6/1 100 2021/7/1 100 2021/8/1 100 2021/9/1 100 2021/10/1 100 2021/11/1 100 2021/12/1 100 2022/1/1 100 2022/2/1 100 2022/3/1 100 2022/4/1 100 2022/5/1 100 2022/6/1 8.79120879120879 2022/7/1 8.79120879120879 2022/8/1 0 2020/5/10 2022/5/9 月 2020/5/1 0 2020/6/1 70 2020/7/1 100 2020/8/1 100 2020/9/1 100 2020/10/1 100 2020/11/1 100 2020/12/1 100 2021/1/1 100 2021/2/1 100 2021/3/1 100 2021/4/1 100 2021/5/1 100 2021/6/1 100 2021/7/1 100 2021/8/1 100 2021/9/1 100 2021/10/1 100 2021/11/1 100 2021/12/1 100 2022/1/1 100 2022/2/1 100 2022/3/1 100 2022/4/1 100 2022/5/1 100 2022/6/1 26.6666666666667 2022/7/1 0 2022/8/1 0 2020/5/10 2022/5/9 季 2020/5/1 0 2020/6/1 0 2020/7/1 0 2020/8/1 90.1098901098901 2020/9/1 0 2020/10/1 0 2020/11/1 100 2020/12/1 0 2021/1/1 0 2021/2/1 100 2021/3/1 0 2021/4/1 0 2021/5/1 100 2021/6/1 0 2021/7/1 0 2021/8/1 100 2021/9/1 0 2021/10/1 0 2021/11/1 100 2021/12/1 0 2022/1/1 0 2022/2/1 100 2022/3/1 0 2022/4/1 0 2022/5/1 100 2022/6/1 8.79120879120879 2022/7/1 0 2022/8/1 0 2020/5/10 2022/5/9 半年 2020/5/1 0 2020/6/1 0 2020/7/1 0 2020/8/1 0 2020/9/1 0 2020/10/1 0 2020/11/1 95.0819672131148 2020/12/1 0 2021/1/1 0 2021/2/1 0 2021/3/1 0 2021/4/1 0 2021/5/1 100 2021/6/1 0 2021/7/1 0 2021/8/1 0 2021/9/1 0 2021/10/1 0 2021/11/1 100 2021/12/1 0 2022/1/1 0 2022/2/1 0 2022/3/1 0 2022/4/1 0 2022/5/1 100 2022/6/1 4.37158469945355 2022/7/1 0 2022/8/1 0 2020/5/10 2022/5/9 年 2020/5/1 0 2020/6/1 0 2020/7/1 0 2020/8/1 0 2020/9/1 0 2020/10/1 0 2020/11/1 0 2020/12/1 0 2021/1/1 0 2021/2/1 0 2021/3/1 0 2021/4/1 0 2021/5/1 97.5274725274725 2021/6/1 0 2021/7/1 0 2021/8/1 0 2021/9/1 0 2021/10/1 0 2021/11/1 0 2021/12/1 0 2022/1/1 0 2022/2/1 0 2022/3/1 0 2022/4/1 0 2022/5/1 100 2022/6/1 2.1978021978022 2022/7/1 0 2022/8/1 0 只有本站会员才能查看附件,请 登录 |
已经使用VBA实现了部分功能,但有部分情况下计算的结果不正确,具体情况是当结算周期为季度时,合同开始日期中的月份大于等于4月时,计算结果为0,结算周期为半年结时,合同开始日期中的年份为以前年度时,计算结果为0,希望能得到各位大神的帮助
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录