我使用以下代码从数据中计算余额,Balance = Demand - collection
代码:
Sub DCB()
Dim c As Range
Dim lRow As Long
lRow = 1
Dim lRowLast As Long
Dim lRowDiff As Long
Dim lRowPortion As Long
lRowPortion = 1
Dim bFoundCollection As Boolean
With ActiveSheet
lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
Do
Set c = .Range("A" & lRow)
If c.Value Like "*COLLECTION*" Then
bFoundCollection = True
ElseIf bFoundCollection Then
bFoundCollection = False
If c.Value <> "BALANCE" Then
c.EntireRow.Insert
lRowLast = lRowLast + 1
Set c = c.Offset(-1, 0)
c.Value = "BALANCE"
End If
If c.Value = "BALANCE" Then
.Range(c, c.Offset(0, 18)).Font.Color = RGB(0, 0, 0)
.Range(c, c.Offset(0, 18)).Interior.Color = RGB(200, 200, 200)
lRowDiff = c.Row - lRowPortion
.Range(c.Offset(0, 3), c.Offset(0, 18)).FormulaR1C1 = _
"=SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*DEMAND*"", R[-" & lRowDiff & "]C:RC)" & _
"-SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*COLLECTION*"", R[-" & lRowDiff & "]C:RC)"
lRowPortion = c.Row + 1
End If
End If
lRow = lRow + 1
Loop While lRow <= lRowLast + 1
End With
End Sub
但我现在遇到一些问题,在计算余额时有一些条件要适用。列U W Y是超额金额列,这些列的值分别依赖于I N S列余额。
例如在I1中,需求是50个集合是10,因此根据公式,Balance = Demand - 集合因此50-10 = 40,但是U列中有值,我希望其余的40个金额应从中扣除U柱&amp;余额应显示0,调整后的40金额应显示在T1列上。
我想调整上面的宏,首先应该检查I2的值,如果I2的值大于I1那么没问题,简单的公式将应用B = DC,但是如果I2的值小于I1或I2的值为0,然后它将检查U1中是否有某个值,如果有一些值,那么该数量将相应调整,调整后的数量应显示在T1中。
答案 0 :(得分:1)
我认为这就是你想要的:
Sub DCB()
Dim c As Range
Dim lRow As Long
lRow = 1
Dim lRowLast As Long
Dim lRowDiff As Long
Dim lRowPortion As Long
lRowPortion = 1
Dim bFoundCollection As Boolean
Dim lRowLastDemand As Long
With ActiveSheet
lRowLast = .Cells(.Rows.Count, 1).End(xlUp).row
Do
Set c = .Range("A" & lRow)
If c.Value Like "*COLLECTION*" Then
bFoundCollection = True
ElseIf c.Value Like "*DEMAND*" Then
lRowLastDemand = lRow
ElseIf bFoundCollection Then
bFoundCollection = False
If c.Value <> "BALANCE" Then
c.EntireRow.Insert
lRowLast = lRowLast + 1
Set c = c.Offset(-1, 0)
c.Value = "BALANCE"
End If
If c.Value = "BALANCE" Then
.Range(c, c.Offset(0, 18)).Font.Color = RGB(0, 0, 0)
.Range(c, c.Offset(0, 18)).Interior.Color = RGB(200, 200, 200)
lRowDiff = c.row - lRowPortion
.Range(c.Offset(0, 3), c.Offset(0, 18)).FormulaR1C1 = _
"=SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*DEMAND*"", R[-" & lRowDiff & "]C:RC)" & _
"-SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*COLLECTION*"", R[-" & lRowDiff & "]C:RC)"
If ((.Cells(lRow, 9)) > 0) And (.Cells(lRowLastDemand, 21) > 0) Then 'If Column I Balance > 0 and U has value
.Cells(lRowLastDemand, 20) = .Cells(lRow, 9) 'T1 = Balance
.Cells(lRowLastDemand, 21) = .Cells(lRowLastDemand, 21) - .Cells(lRow, 9) 'Adjust Col U
End If
If ((.Cells(lRow, 14)) > 0) And (.Cells(lRowLastDemand, 23) > 0) Then 'If Column N Balance > 0 and W has value
.Cells(lRowLastDemand, 22) = .Cells(lRow, 14) 'V1 = Balance
.Cells(lRowLastDemand, 23) = .Cells(lRowLastDemand, 23) - .Cells(lRow, 14) 'Adjust Col W
End If
If ((.Cells(lRow, 19)) > 0 And (.Cells(lRowLastDemand, 25) > 0)) Then 'If Column S Balance > 0 and Y has value
.Cells(lRowLastDemand, 24) = .Cells(lRow, 19) 'X1 = Balance
.Cells(lRowLastDemand, 25) = .Cells(lRowLastDemand, 25) - .Cells(lRow, 19) 'Adjust Col Y
End If
lRowPortion = c.row + 1
End If
End If
lRow = lRow + 1
Loop While lRow <= lRowLast + 1
End With
End Sub