我试图列出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
(图中左边的那个是我现在拥有的,我正试着去右边的照片。谢谢:))
答案 0 :(得分: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