Excel中有多个有条件的VBmacros,需要进行简单的代码修改

我正在使用以下代码从数据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 

但是我现在有一些问题,在计算平衡时有一些条件可以应用。 UWY列是超额金额列,这些列的值分别取决于INS列平衡金额。

例如,在I1中,需求是50个集合是10,所以按照公式,Balance = Demand – 因此50-10 = 40,但是U列中有值,我想从U列中扣除剩下的40个数量&平衡应该显示0和调整后的40应该显示在T1列。

我想要调整上面的macros,首先I2的值应该检查,如果I2的值大于I1那么没问题,简单的公式会应用B = DC,但是如果I2的值小于I1或者I2的值为0,则会检查U1中是否有值,如果有值,则相应的调整量,并在T1中显示调整量。

同样,N列与W列相关联,调整量将显示在V列&S列与Y列相关联,并且调整量将显示在X列上。 请检查图像

我想这是你想要的:

  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