因此,我在下面的代码尝试在H列中找到WIP。如果我们找到WIP:复制3个单元格并在下一列中将它们分别放在同一行或下一个可用行中。
由于某种原因,代码只对第一个“WIP”值成功运行循环,然后给出代码中断错误。有人能看出为什么会这样吗?
谢谢你,Ori
Sub Step1_update()
Dim dblSKU As Double
Dim strDesc As String
Dim strType As String
Dim BrowFin As Integer
Dim Browfin1 As Integer
Dim Counter As Integer
Dim Trowfin As Integer
Counter = 0
Worksheets("Final").Activate
Trowfin = 5
BrowFin = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'loop 1
Do While Trowfin < BrowFin
'If 1 (set the 3 values)
If Range("H" & Trowfin).Value = Range("H3").Value Then
dblSKU = Range("F" & Trowfin).Value
strDesc = Range("G" & Trowfin).Value
strType = Range("H" & Trowfin).Value
'Find the last used row in Col J
Browfin1 = (ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row)
Counter = 0
'paste values 15 times
Do While Counter < 15
'If 2
If Browfin1 > (Trowfin + Counter) Then
Range("J" & Browfin1).Value = dblSKU
Range("K" & Browfin1).Value = strDesc
Range("L" & Browfin1).Value = strType
ElseIf Browfin1 < (Trowfin + Counter) Then
Range("J" & (Trowfin + Counter)).Value = dblSKU
Range("K" & (Trowfin + Counter)).Value = strDesc
Range("L" & (Trowfin + Counter)).Value = strType
Else
Range("J" & (Trowfin + Counter)).Value = dblSKU
Range("K" & (Trowfin + Counter)).Value = strDesc
Range("L" & (Trowfin + Counter)).Value = strType
End If
'Loop to paste the WIP 15 times
Loop
Trowfin = Trowfin + 1
Counter = 0
'If cell (H...) is not a WIP
Else
Trowfin = Trowfin + 1
'If 1
End If
'loop 1
Loop
End Sub
答案 0 :(得分:0)
您之前的代码如下:
Else
Range("J" & (Trowfin + Counter)).Value = dblSKU
Range("K" & (Trowfin + Counter)).Value = strDesc
Range("L" & (Trowfin + Counter)).Value = strType
Counter = Counter + 1
'If 2
End If
Loop
你应该这样做:
Else
Range("J" & (Trowfin + Counter)).Value = dblSKU
Range("K" & (Trowfin + Counter)).Value = strDesc
Range("L" & (Trowfin + Counter)).Value = strType
'If 2
End If
Counter = Counter + 1
Loop
您当前的代码在Else
内有增量,这意味着在循环执行时有可能不会被命中。如果发生这种情况,您的循环将无限循环,崩溃Excel或导致代码中断。
如果您想基于计数器进行循环,则需要确保计数器在非无限长的时间内达到退出条件。