gpt4 book ai didi

vba - 嵌套循环和If,不执行循环

转载 作者:行者123 更新时间:2023-12-03 08:54:27 25 4
gpt4 key购买 nike

每次运行代码时,我都会不断遇到“不执行”的循环,看不到丢失任何内容或循环放错位置的情况。
我需要此代码来查找特定列副本中的关键字,然后将其粘贴到“摘要”选项卡中。

您的帮助将不胜感激。

    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

最佳答案

以下部分缺少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

关于vba - 嵌套循环和If,不执行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com