2015-11-04 59 views
0

我有一些'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儘快得到一個無效的下一個控制變量誤差子開始。

我該如何做這項工作?

+1

反轉'下一個K'通過'下一個T'的順序。 –

+0

謝謝@ExcelHero!現在宏正在運行。仍然沒有按照正確的順序,但這是朝着正確方向邁出的一步。 – karhu

回答

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 
+0

非常清楚。謝謝。任何想法,我應該運行我的發言? – karhu

0

如果我讀這正確:

機有多種成品 成品有多個部件 組件有多個屬性

你內心的循環應該處理的組件。接下來的循環應該是成品,下一個循環應該是機器。

要排除故障,請重新啓動並: 獲取用於執行組件的for/next循環。 在此之外添加for/next循環(所以FOR在宏之前,下一個在宏之後)將所有內容擴展爲多個完成的產品 最後在機器的外部添加一個循環