【问题标题】:3 Nested for loop in macro3 在宏中嵌套for循环
【发布时间】:2023-03-22 16:31:01
【问题描述】:

我正在尝试列出 A 列中的所有计划,以及 B 列中每个计划的所有区域。因此,计划 1 与区域 1 和 2,计划 2 与区域 1 和 2等。我的代码目前在 A 列中列出了计划 1、计划 2 等,但它只列出了计划 1 的区域 1 和 2,并且停在那里。您如何继续前进并列出其余计划的区域 1 和区域 2?谢谢你:))

因此,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

(图片左边的就是我现在拥有的,我正在尝试找到右边的图片。谢谢:))

【问题讨论】:

  • 显示您的数据。一张图就够了。
  • 嗨,Masoud,你如何添加图片?谢谢:)
  • 我想我可以使用 YowE3K 的代码来获得我需要的东西。谢谢大家!

标签: vba for-loop nested


【解决方案1】:

鉴于您拥有的循环数,听起来您最好只为输出行使用计数器。

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

【讨论】:

  • 这真的很有帮助!!当有 3 个循环时,它是否完成了最内层的循环并重新开始第一个循环?还是移动到第二个循环?谢谢 ! :)
  • @JBB - 每次到达Next 语句时,该循环的计数器都会增加,然后控制返回到循环中的第一个语句(如果您没有超过结束值)或到以下语句(如果有)。所以在上面的例子中最里面的循环结束后,控制转移到中间循环的Next,它的计数器增加,最里面的循环可能会再次执行。一旦最内层循环在中间循环中最后一次结束,最外层循环的计数器就会增加,然后中间循环再次开始,等等。
  • 我明白了。谢谢!
  • @JBB 我刚刚在答案的末尾添加了一个非常简单的示例 - 尝试运行它(或者更好的是,使用 F8 逐行遍历它)它可以帮助您理解循环更好。
  • 哦,太好了!谢谢你:)
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多