我已经创建了一个VBA代码,以计算应收帐款的按年龄分类的报告,如下图所示。但是完成计算花费的时间太长。 如果您能帮助我优化此代码以缩短计算时间,将不胜感激。
Excel工作表如下所示:
Sub ARAgingSummary()
Dim StartRow As Long
Dim TotalDue As Double
Dim BalanceDue As Double
Dim L30 As Double
Dim L60 As Double
Dim L90 As Double
Dim L120 As Double
Dim O120 As Double
Dim OpeningBal As Double
Dim AL30 As Range
Dim AL60 As Range
Dim AL90 As Range
Dim AL120 As Range
Dim AO120 As Range
Dim AOpeningBal As Range
StartRow = 5
Do Until Range("I" & StartRow) = ""
Do Until Range("I" & StartRow) = ""
TotalDue = Range("I" & StartRow).Value
L30 = Range("B" & StartRow).Value
L60 = Range("C" & StartRow).Value
L90 = Range("D" & StartRow).Value
L120 = Range("E" & StartRow).Value
O120 = Range("F" & StartRow).Value
OpeningBal = Range("G" & StartRow).Value
Set AL30 = Range("J" & StartRow)
Set AL60 = Range("K" & StartRow)
Set AL90 = Range("L" & StartRow)
Set AL120 = Range("M" & StartRow)
Set AO120 = Range("N" & StartRow)
Set AOpeningBal = Range("O" & StartRow)
If L30 <= TotalDue Then
AL30.Value = L30
BalanceDue = TotalDue - L30
ElseIf L30 > TotalDue Then
AL30.Value = TotalDue
Exit Do
End If
If L60 <= BalanceDue Then
AL60.Value = L60
BalanceDue = BalanceDue - L60
ElseIf L60 > BalanceDue Then
AL60.Value = BalanceDue
Exit Do
End If
If L90 <= BalanceDue Then
AL90.Value = L90
BalanceDue = BalanceDue - L90
ElseIf L90 > BalanceDue Then
AL90.Value = BalanceDue
Exit Do
End If
If L120 <= BalanceDue Then
AL120.Value = L120
BalanceDue = BalanceDue - L120
ElseIf L120 > BalanceDue Then
AL120.Value = BalanceDue
Exit Do
End If
If O120 <= BalanceDue Then
AO120.Value = O120
BalanceDue = BalanceDue - O120
ElseIf O120 > BalanceDue Then
AO120.Value = BalanceDue
Exit Do
End If
If OpeningBal <= BalanceDue Then
AOpeningBal.Value = OpeningBal
BalanceDue = BalanceDue - OpeningBal
Exit Do
ElseIf OpeningBal > BalanceDue Then
AOpeningBal.Value = BalanceDue
Exit Do
End If
Loop
BalanceDue = 0
StartRow = StartRow + 1
Loop
End Sub
就计算而言,这很好用,但是计算时间很长。