【问题标题】:Nested Loops & Ifs, loop without Do嵌套循环和如果,循环不做
【发布时间】:2015-10-14 03:15:02
【问题描述】:

每次我运行我的代码时,我都会得到一个没有 Do 的循环,但我看不到我在哪里丢失了任何东西,或者循环是否放错了位置。 我需要此代码来查找特定列复制中的关键字,然后将它们粘贴到摘要选项卡中。

您的帮助将不胜感激。

    Sub Summary()

Dim MainLoop As Double
Dim SecondLoop As Double
Dim thirdLoop As Double
Dim Trow As Double
Dim counter As Integer

Dim PSKU As Integer
Dim PDesc As String
Dim PPKG As Integer

Dim CSKU As Integer
Dim CDesc As String
Dim CPKG As Integer
Dim Cstatus As String

MainLoop = 5
SecondLoop = 0
thirdLoop = 0
Trow = 5
counter = 0

Worksheets("final").Activate

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

        ParentSKU = Range("F" & MainLoop).Value
        ParentDesc = Range("G" & MainLoop).Value

        Worksheets("Summary").Activate

            SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
            Range("A" & SumRow).Value = ParentSKU
            Range("B" & SumRow).Value = ParentDesc
            Range("C" & SumRow).Value = "Parent"

            Worksheets("Final").Activate

                Do While SecondLoop < 20

                    If Range("H" & MainLoop + SecondLoop).Value = "MAT" Or "PKG" Or "ING" Then

                       Range("F" & MainLoop + SecondLoop).Value = CSKU
                       Range("G" & MainLoop + SecondLoop).Value = CDesc
                       Range("H" & MainLoop + SecondLoop).Value = (Cstatus)
                       Range("I" & MainLoop + SecondLoop).Value = CPKG

                       Worksheets("Summary").Activate

                            SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
                            Range("A" & SumRow).Value = CSKU
                            Range("B" & SumRow).Value = CDesc
                            Range("C" & SumRow).Value = "Child"
                            Range("D" & SumRow).Value = CPKG


                     ElseIf Range("H" & MainLoop + SecondLoop).Value = "WIP" Then

                        Find = Range("F" & MainLoop + SecondLoop).Value

                        Do While Trow < ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row & thirdLoop < 20

                            If Range("J" & Trow).Value = Find Then

                                    If Range("P" & Trow + thirdLoop).Value <> "" Then

                                        CSKU = Range("P" & Trow + thirdLoop).Value
                                        CDesc = Range("Q" & Trow + thirdLoop).Value
                                        Cstatus = Range("R" & Trow + thirdLoop).Value
                                        CPKG = Range("S" & Trow + thirdLoop).Value

                                        Worksheets("Summary").Activate

                                            SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
                                            Range("A" & SumRow).Value = CSKU
                                            Range("B" & SumRow).Value = CDesc
                                            Range("C" & SumRow).Value = "Child"
                                            Range("D" & SumRow).Value = CPKG

                                            Worksheets("Final").Activate

                                        thirdLoop = thirdLoop + 1
                                        Trow = Trow + 1

                                    Else

                                        Trow = Trow + 1

                                    End If

                           Else
                            thirdLoop = thirdLoop + 1
                           End If

                           Loop


                    End If

                    SecondLoop = SecondLoop + 1


                    MainLoop = MainLoop + 20

                Loop

                Worksheets("Final").Activate

End Sub

【问题讨论】:

标签: vba loops if-statement error-handling do-while


【解决方案1】:

以下部分缺少End If

                              If Range("P" & Trow + thirdLoop).Value <> "" Then

                                    CSKU = Range("P" & Trow + thirdLoop).Value
                                    CDesc = Range("Q" & Trow + thirdLoop).Value
                                    Cstatus = Range("R" & Trow + thirdLoop).Value
                                    CPKG = Range("S" & Trow + thirdLoop).Value

                                    Worksheets("Summary").Activate

                                        SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
                                        Range("A" & SumRow).Value = CSKU
                                        Range("B" & SumRow).Value = CDesc
                                        Range("C" & SumRow).Value = "Child"
                                        Range("D" & SumRow).Value = CPKG

                                        Worksheets("Final").Activate

                                    thirdLoop = thirdLoop + 1
                                    Trow = Trow + 1
                                 'missing end if here



                                 Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多