嵌套如果不会触发ElseIf语句

时间:2015-07-27 20:02:03

标签: vba if-statement do-while

此代码循环遍历一列数据。如果列中的单元格是WIP,则它将转到相应的列并循环遍历该数据。如果没有,它将获取数据并将其粘贴到摘要选项卡中。但由于某种原因,只有当单元格是WIP而忽略其余部分时才会发现它。

有人可以看到为什么代码不会触发我的第二个IF。

    Sub summary_2()

    Dim MainLoop As Double
    Dim Secondloop As Double
    Dim TopRow As Double
    Dim ThirdLoop As Double

    Dim ParentName As String
    Dim ParentSku As Double
    Dim WipSku As String

    MainLoop = 5
    TopRow = 5

    Worksheets("Final").Activate

    Do While MainLoop < ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

        ParentSku = Range("A" & MainLoop)
        ParentName = Range("B" & MainLoop)

        Worksheets("Summary 2").Activate

        Range("A" & (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 2) = ParentSku
        Range("B" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 2) = ParentName
        Range("C" & (ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row) + 2) = "Parent"
        Range("D" & (ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row) + 2) = " - "

        Worksheets("Final").Activate

        Do While Secondloop < 20

            'This If gets Triggered
            If Range("H" & (MainLoop + Secondloop)) = "WIP" Then

                WipSku = Range("F" & (MainLoop + Secondloop))

                Do While TopRow < ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row

                    If Range("J" & TopRow) = WipSku Then

                        Do While ThirdLoop < 10

                            childSKU = Range("P" & (TopRow + ThirdLoop))
                            childDesc = Range("Q" & (TopRow + ThirdLoop))
                            childType = Range("R" & (TopRow + ThirdLoop))
                            childPKG = Range("S" & (TopRow + ThirdLoop))

                            Worksheets("Summary 2").Activate

                            Range("A" & (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1) = childSKU
                            Range("B" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 1) = childDesc
                            Range("C" & (ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row) + 1) = childType
                            Range("D" & (ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row) + 1) = childPKG

                            Worksheets("Final").Activate

                            ThirdLoop = ThirdLoop + 1
                        Loop

                    ElseIf Range("J" & TopRow) <> WipSku Then

                        TopRow = TopRow + 1

                    End If

                Loop


                Worksheets("Final").Activate

            'This if does not get triggered even if its true.
            ElseIf Range("H" & (MainLoop + Secondloop)) = "ING" Or Range("H" & (MainLoop + Secondloop)) = "MAT" Or Range("H" & (MainLoop + Secondloop)) = "PKG" Then

                childSKU = Range("F" & MainLoop + Secondloop)
                childDesc = Range("G" & MainLoop + Secondloop)
                childType = Range("H" & MainLoop + Secondloop)
                childPKG = Range("I" & MainLoop + Secondloop)

                Worksheets("Summary 2").Activate

                Range("A" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 1) = childSKU
                Range("B" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 1) = childDesc
                Range("C" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 1) = childType
                Range("D" & (ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row) + 1) = childPKG

             Worksheets("Final").Activate

            End If

        Loop

        MainLoop = MainLoop + 20
    Loop

End Sub

1 个答案:

答案 0 :(得分:0)

您没有在sub中的任何位置设置“SecondLoop”。因此它没有增加。看起来这应该只是永久地运行,因为Do While Secondloop < 20永远不会“不真实”,因为从零开始并保持在那里。

如果没有查看数据是否会导致您的确切问题,很难说,但这绝对是一个问题。