在For循环中动态更新变量?

时间:2018-01-05 01:39:07

标签: .net excel vba

我正在尝试创建一个循环遍历行的循环,并且:(a)在循环列中的新单元格值触发时刷新变量,或者(b)在满足条件时更新其值并将其带入循环的下一次迭代。

我的Excel表看起来基本上是这样的:

Header  commit  Avail   Comment
SKU1    6       5       Over
SKU1    3       5       Not Over (2 remaining)
SKU1    3       5       Over
SKU1    2       5       Not Over (0 remaining)
SKU1    3       5       Over
SKU1    3       5       Over
SKU2    3       4       Not Over (1 remaining)
SKU2    2       4       Over

感谢任何帮助。这是我到目前为止所拥有的。我遇到的问题是它进入下一个循环迭代时似乎没有存储更新值(AvailableInv = AvailableInv - LineCommit):

Sub Macro1()
'
' Macro1 Macro
'
Dim max As Integer
Dim PrevSKU As String
Dim CurrSKU As String
Dim AvailableInv As Integer
Dim LineCommit As Integer
Dim loopct As Integer

PrevSKU = Worksheets("New").Cells(2, "A").Value
AvailableInv = Worksheets("New").Cells(2, "C").Value
max = ActiveSheet.UsedRange.Rows.Count

For loopct = 2 To max

    CurrSKU = Worksheets("New").Cells(loopct, "A").Value
    LineCommit = Worksheets("New").Cells(loopct, "B").Value

    If CurrSKU <> PrevSKU Then
        AvailableInv = Worksheets("New").Cells(loopct, "C").Value
    Else
        PrevSKU = Worksheets("New").Cells(loopct - 1, "A").Value
    End If

    If LineCommit >= AvailableInv Then
        Worksheets("New").Cells(loopct, "D").Value = "Over"
    Else
        AvailableInv = AvailableInv - LineCommit
        Worksheets("New").Cells(loopct, "D").Value = "Not Over (" & AvailableInv & " remaining)"
    End If


Next loopct

1 个答案:

答案 0 :(得分:0)

如果您想要的是这样的表格,您可以按照表格后面的代码进行操作:

Header  commit  Avail   Comment
SKU1    1       5       Not Over (4 remaining)
SKU1    3       5       Not Over (1 remaining)
SKU1    3       5       Over
SKU1    2       5       Over
SKU1    3       5       Over
SKU1    3       5       Over
SKU2    3       4       Not Over (1 remaining)
SKU2    2       4       Over

代码示例:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Dim max As Integer
    Dim PrevSKU As String
    Dim CurrSKU As String
    Dim AvailableInv As Integer
    Dim LineCommit As Integer
    Dim loopct As Integer

    PrevSKU = Worksheets("New").Cells(2, "A").Value
    AvailableInv = Worksheets("New").Cells(2, "C").Value
    max = ActiveSheet.UsedRange.Rows.Count

    For loopct = 2 To max

        CurrSKU = Worksheets("New").Cells(loopct, "A").Value
        LineCommit = Worksheets("New").Cells(loopct, "B").Value

        If loopct > 2 Then PrevSKU = Worksheets("New").Cells(loopct - 1, "A").Value

        If CurrSKU <> PrevSKU Then
            AvailableInv = Worksheets("New").Cells(loopct, "C").Value
             Debug.Print ("Loop:" & loopct & "   CuuSKU:" & CurrSKU & "    PrevSKU:" & PrevSKU & "    Available:" & AvailableInv)
        End If


        AvailableInv = AvailableInv - LineCommit

        If AvailableInv < 1 Then
            Worksheets("New").Cells(loopct, "D").Value = "Over"
        Else

            Worksheets("New").Cells(loopct, "D").Value = "Not Over (" & AvailableInv & " remaining)"
        End If


    Next loopct
End Sub

基本上你在宏中有一些逻辑错误。另一个建议是使用Dubug.Print输出变量并检查它们。