lundi 27 juillet 2015

Nested If wont trigger ElseIf statement

This code loops through a column of data. If the cell in the column Is a WIP it goes to a corresponding column and loops through that data. If not it takes the data and pastes it in the summary tab. Yet for some reason it only picks up if the cell is a WIP and ignores the rest.

Can someone see why the code does not trigger my second IF.

Cheers

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).Value
    ParentName = Range("B" & MainLoop).Value

    Worksheets("Summary 2").Activate


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

    Worksheets("Final").Activate

    Do While Secondloop < 20

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

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

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

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


                    Do While ThirdLoop < 10

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

                       Worksheets("Summary 2").Activate

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

                        Worksheets("Final").Activate

                    ThirdLoop = ThirdLoop + 1
                    Loop

                ElseIf Range("J" & TopRow).Value <> 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).Value
            childDesc = Range("G" & MainLoop + Secondloop).Value
            childType = Range("H" & MainLoop + Secondloop).Value
            childPKG = Range("I" & MainLoop + Secondloop).Value

            Worksheets("Summary 2").Activate

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

             Worksheets("Final").Activate



        End If
    'Loop

    Loop




    MainLoop = MainLoop + 20
Loop


End Sub

Aucun commentaire:

Enregistrer un commentaire