我有很多' For Next'声明,但我无法按照我希望的顺序运行它们。基本上填充了四个单元格,在行号索引中添加了八行(从上一个产品开始的位置开始),然后再次填充四个单元格:
然后我们将1192个单元格跳转到下一台机器并再次启动该过程:
Prefix serialized JSON with
以下是我认为代码应该如何运行的方式。 (我在这里的结构可能完全错了):
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
我在子{2}开始时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
出现无效的下一个控制变量错误。
我该如何做到这一点?
答案 0 :(得分:3)
您必须按照打开它的相反顺序关闭For Next。
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
答案 1 :(得分:0)
如果我正确读到这个:
机器有多个成品 成品有多个组件 组件具有多个属性
你最内层的循环应该处理组件。下一个循环应该是成品,下一个循环应该是机器。
要进行问题排查,请启动新的和: 获取for / next循环工作来执行组件。 在这之外添加一个for / next循环(所以FOR在它之前是宏,下一个在它之后)将所有内容扩展为多个完成的prodocuts 最后在机器
之外添加一个循环