2017-06-07 13 views
0

enter image description here私は、列Aのすべての計画を列挙し、列Bの各計画のすべての領域を列挙しようとしています。したがって、計画1と領域1と2、計画2はエリア1と2など。私のコードは現在、プラン1、プラン2などを列Aにリストアップしていますが、プラン1のエリア1と2をリストしています。あなたはそれを継続して残りの計画についてエリア1とエリア2を挙げるためにどのようにそれを作っていますか?ありがとう:))3ネストされたforループのマクロ

したがって、列1は計画1、計画2などを順番に持つことになります。そして、列Bは、などのエリア1、エリア2、エリア1、エリア2、

'List out all areas for all plans 

For Plan = 0 To 5 

    'List out all areas within a plan 

For Area = 0 To 6 

    'List out one Area 

    For Row = 2 To 10 
     Sheets("S").Cells(Area * 51 + Row, 2) = Sheets("S").Cells(Area + 2, 31) 
     Next Row 
Next Area 
Next Plan 

(写真の左側の1は私が今持っているものである、と私は絵に取得しようとしているだろうありがとう:))

+1

表示あなたのデータ。画像で十分でしょう。 – Masoud

+0

こんにちはマサウッド、どのように写真を追加しますか?ありがとう:) – JBB

+0

https://meta.stackexchange.com/questions/83096/how-to-place-an-image-in-a-stack-overflow-question – Masoud

答えて

3

あなたが持っているループの数を考えると、出力行にカウンタを使用するほうが良いでしょう。

またOutputRow

Dim FirstPlan As Long: FirstPlan = 0 
Dim LastPlan As Long: LastPlan = 5 
Dim Plan As Long 

Dim FirstArea As Long: FirstArea = 0 
Dim LastArea As Long: LastArea = 6 
Dim Area As Long 

Dim FirstRow As Long: FirstRow = 2 
Dim LastRow As Long: LastRow = 10 
Dim myRow As Long ' Avoid "Row" as a variable name 
Dim OutputRow As Long 

OutputRow = 2 'Specify first row to be written to 

'List out all areas for all plans 
For Plan = FirstPlan To LastPlan 

    'List out all areas within a plan 
    For Area = FirstArea To LastArea 

     'List out one Area 
     For myRow = FirstRow To LastRow     
      'I'm guessing at this line 
      Sheets("S").Cells(OutputRow, "A").Value = _ 
        Sheets("S").Cells(Plan - FirstPlan + 2, "AD").Value 

      Sheets("S").Cells(OutputRow, "B").Value = _ 
        Sheets("S").Cells(Area - FirstArea + 2, "AE").Value 

      'I'm guessing at this line 
      Sheets("S").Cells(OutputRow, "C").Value = _ 
        Sheets("S").Cells(myRow - FirstRow + 2, "AF").Value 

      'Set up ready for the next row to be written 
      OutputRow = OutputRow + 1 
     Next 
    Next 
Next 

はそれが必要とされるたびに計算することができます:あなたは、ループの制御フローに使用されていない場合は

Dim FirstPlan As Long: FirstPlan = 0 
Dim LastPlan As Long: LastPlan = 5 
Dim Plan As Long 

Dim FirstArea As Long: FirstArea = 0 
Dim LastArea As Long: LastArea = 6 
Dim Area As Long 

Dim FirstRow As Long: FirstRow = 2 
Dim LastRow As Long: LastRow = 10 
Dim myRow As Long ' Avoid "Row" as a variable name 
Dim OutputRow As Long 

'List out all areas for all plans 
For Plan = FirstPlan To LastPlan 

    'List out all areas within a plan 
    For Area = FirstArea To LastArea 

     'List out one Area 
     For myRow = FirstRow To LastRow     
      OutputRow = ((Plan - FirstPlan) * (LastArea - FirstArea + 1) + _ 
         (Area - FirstArea)) * (LastRow - FirstRow + 1) + _ 
         (myRow - FirstRow) + 2 

      'I'm guessing at this line 
      Sheets("S").Cells(OutputRow, "A").Value = _ 
        Sheets("S").Cells(Plan - FirstPlan + 2, "AD").Value 

      Sheets("S").Cells(OutputRow, "B").Value = _ 
        Sheets("S").Cells(Area - FirstArea + 2, "AE").Value 

      'I'm guessing at this line 
      Sheets("S").Cells(OutputRow, "C").Value = _ 
        Sheets("S").Cells(myRow - FirstRow + 2, "AF").Value 

     Next 
    Next 
Next 

を、で次の例を実行してみてください空のワークシート:

Dim colA As Long, colB As Long, colC As Long 
Dim r As Long 
With ActiveSheet 
    For colA = 11 To 12 
     For colB = 21 To 23 
      For colC = 31 To 34 
       r = r + 1 
       .Cells(r, "A").Value = colA 
       .Cells(r, "B").Value = colB 
       .Cells(r, "C").Value = colC 
      Next colC 
'The following statement will be executed AFTER processing colC as 34 
     Next colB 
'The following statement will be executed AFTER processing colB as 23 
    Next colA 
'The following statement will be executed AFTER processing colA as 12 
End With 
+0

これは本当に役に立ちます!! 3つのループがある場合は、最も内側のループを終了し、最初のループをもう一度開始しますか?それとも2番目のループに移動しますか?ありがとうございました ! :) – JBB

+0

@JBB - 「Next」文に達するたびに、そのループのカウンタがインクリメントされ、制御がループ内の最初の文に戻ります(終了値を超えていない場合)。または次の文に戻りますあなたが持っている場合)。上の例で最も内側のループが終了した後、制御は中間ループの 'Next'に移動し、カウンタがインクリメントされ、最も内側のループが再び実行されます。一番内側のループが途中のループで最後に終了すると、最も外側のループのカウンタがインクリメントされ、もう一度中央のループが開始されます。 – YowE3K

+0

です。ありがとうございました! – JBB

関連する問題