|
各位大神,帮忙看看 怎么解决这个溢出的问题?谢谢!
Sub 分解回款金额()
Dim i As Integer, j As Integer, k As Integer
Dim start_date As Date, end_date As Date
Dim payment_amt As Double, year_payment As Double
Dim this_year As Integer, last_year As Integer, next_year As Integer
' 获取当前年份
this_year = Year(Date)
' 取得数据范围
Dim data_range As Range
Set data_range = Range("A2:C10") ' 假设数据在A2:C10
For i = data_range.Rows.Count To 1 Step -1
' 获取该行数 据
start_date = data_range(i, 1).Value
end_date = data_range(i, 2).Value
payment_amt = data_range(i, 3).Value
' 计算总月份数
total_months = (Year(end_date) - Year(start_date)) * 12 + (Month(end_date) - Month(start_date))
' 计算每月应收款额
monthly_payment = payment_amt / total_months
' 分解回款
For j = 0 To total_months - 1
year_payment = 0
If (Month(start_date + j * 30) > 12) Then
' 下一年
next_year = this_year + 1
year_payment = monthly_payment
ElseIf (Year(start_date + j * 30) < this_year) Then
' 上一年
last_year = Year(start_date + j * 30)
year_payment = monthly_payment
Else
' 本年
year_payment = monthly_payment
End If
' 将回款金额写入对应的单元格
Cells(i + 1, j + 4).Value = year_payment
Next j
Next i
End Sub
|
|