多重条件VB宏在Excel中,简单的代码需要修改
我使用下面的代码来计算的数据平衡,平衡=需求 - 收集多重条件VB宏在Excel中,简单的代码需要修改
代码:
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 - collection因此50-10 = 40,但是U列中有值,我想剩下的40个数量应该被扣除从U列&余额应显示0和调整后的40应该显示在T1列。
我想要对上面的宏进行调整,首先应该检查I2的值,如果I2的值大于I1则无问题,简单的公式将应用B = DC,但如果I2的值较小比I1或I2的值为0,则会检查U1中是否有值,如果有一些值,那么将相应地调整该量,并在T1中显示调整量。
我想这是你想要什么:
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
感谢快速回复 我有两个问题 当U是100 1.在I3中运行您的宏后,我得到Rs 40,我想零和T1是40伟大和U1是60伟大。 我希望余额I3为零,因为Rs 40的金额已经用U1的值进行了调整。 UW Ycolumn是多余的专栏,消费者提前付款给我们,以便如果他从Rs 50支付了10卢比,那么我们应该有余额40,但他已经为我们预先支付了100卢比,这就是为什么Rs 40需要调整&应显示在T1上并且您的代码正在工作,但我需要I3列显示零 –
2.当U 1的值小于余额 例如I1需求为50 I2收集为10并且余额显示为40,但U1价值10然后那Rs 10需要被调整U1应该是0,但与您的代码我得到U1 -30。 &I3应该是50-集合10&调整10因此I 3应该是30&T1应该是10 同样的原则应该适用于与UWY列相关的INS列 –
尝试使用调试器逐步执行代码。您将能够看到这些值是什么以及if语句如何工作。 – dev1998
这不是VB.NET代码。它看起来像某种形式的VBA – Plutonix