2017-06-07 79 views
0

enter image description here我想列出列A中的所有计划以及列B中每个计划的所有区域。所以这将是计划1与区域1和2,计划2与区域1和2等。我的代码目前在列A中列出了计划1,计划2等,但它只列出计划1的区域1和2,并且它在那里停止。你如何使它继续下去,并列出其他计划的区域1和区域2?谢谢:))3在宏中嵌套for循环

因此,列A将有计划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

显示你的数据。一张照片就足够了。 – Masoud

+0

嗨马苏德,你如何添加图片?谢谢:) – JBB

+0

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

回答

3

鉴于你有多少个循环,这听起来像你最好只使用一个计数器作为你的输出行。

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 

或者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 

'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个循环时,它是否完成了最内部循环并重新开始第一个循环?或者它移动到第二个循环?谢谢 ! :) – JBB

+0

@JBB - 每次达到“Next”语句时,该循环的计数器都会递增,然后控制权返回循环内的第一条语句(如果您尚未超出最终值)或返回至以下语句(如果你有)。因此,在上面的例子中最内层的循环结束之后,控制移动到中间循环的“下一个”并且其计数器递增,最内层的循环可能会再次执行。一旦最内层循环在中间循环中最后一次结束,则最外层循环的计数器递增,中间循环再次开始,等等。 – YowE3K

+0

我明白了。谢谢! – JBB