我应该如何嵌套我的“For Next”语句?

我有一些“For Next”语句,但是我不能让它们以我想要的顺序运行。 基本上四个单元格被填充,八行被添加到行号索引(从上一个产品开始的地方开始),然后再填充四个单元格:

1

然后我们跳1192个单元格到下一个机器,然后再次开始这个过程:

2

1. Machine 1 Component 0 Attribute 1 2. Machine 1 Component 0 Attribute 2 ... 5. Machine 1 Component 1 Attribute 1 6. Machine 1 Component 1 Attribute 2 ... 9. Machine 2 Component 0 Attribute 1 10. Machine 2 Component 0 Attribute 2 ... 13. Machine 2 Component 1 Attribute 1 

这是我相信代码应该运行的方式。 (我可能对这里的结构完全错误):

 cycle through array of attributes K increment component number I increment S "jump 8 cells" increment finished product number R increment machine number M reset finished product number R to 0 increment T "jump 1192 cells" increment T "jump 1192 cells" reset finished product number R to 0 increment machine number M increment finished product number R increment S "jump 8 cells" increment component number I cycle through array of attributes K 

这是我的代码:

 Sub Button1_Click() 'r is the current finished product and starts at 0 r = 0 'k is attribute index. .length, .width, .height, .weight Dim A(): ReDim A(3) A(0) = "Length": A(1) = "Width": A(2) = "Height": A(3) = "Weight" For K = LBound(A) To UBound(A) 'i is part number. 0-19 for 20 components. limited to first part to make output smaller for testing. For i = 0 To 0 's is row offset. 0 to 1184 for 149 finished products. adds 8 to the row index to start each finished product at the right row. For s = 0 To 8 Step 8 'r is current finished product. increments by 1 here r = r + 1 'm is machine index. 1,2,3,8. limited to 2 here to make output smaller for testing. Dim Q(): ReDim Q(1) Q(0) = "1": Q(1) = "2" For m = LBound(Q) To UBound(Q) 'p and t for machine row value offset. need to add 1192 for each machine. limited to 2 here to make output smaller for testing. Dim P(): ReDim P(1) P(0) = "0": P(1) = "1192" For T = LBound(P) To UBound(P) 'this msgbox is helpful for debugging. it shows data stores in the variables for each cycle. MsgBox "m" & Q(m) & " r" & r & " i" & i & " k" & A(K) & " Location: (" & 4 + s + K + P(T) & ", " & 2 + i & ")" ProductName = "machine" & Q(m) & ".finishedProduct[" & r & "].component[" & i & "]." & A(K) If TypeName(Data) = "Error" Then If MsgBox("Error reading machine" & Q(m) & ".finishedProduct[" & r & "].Component[" & i & "]. " & _ "Continue with read?", vbYesNo + vbExclamation, _ "Error") = vbNo Then Exit For Else 'no error, put data in cell End If '0=b4,1=c4,3=d4, etc. FYI 2,4 = D2 so b4= 4,2 (+0 because first i value) 'writes result to cell. second iteration should start 8 rows below end of previous Cells(4 + s + K + P(T), 2 + i) = ProductName 'increment attribute name Next K 'increment component number Next i 'increment row offset for finished product Next s 'increment machine number Next m 'increment machine row offset (1190 rows) Next T End Sub 

一旦子开始,我在下 Next K处得到无效的下一个控制variables错误。

我怎样才能做这个工作?

您必须以与您打开它相反的顺序closures“下一步”。

 For k = LBound(A) To UBound(A) ... For J = 1 To 9 ... For m = 1 To 8 step 2 .... Next m For n = 1 To 9 step 3 .... Next n Next J Next k 

如果我正确阅读这个:

机器具有多个成品成品具有多个部件部件具有多个属性

你最内层的循环应该处理组件。 下一个循环应该是成品,下一个循环应该是机器。

要排除故障,开始新的和:获取for / next循环工作做组件。 在这个之外添加一个for / next循环(所以FOR在macros之前,下一个在macros之后)将所有东西展开成多个完成的产品最后在机器外部添加一个循环