在循环中更改For循环的长度

我有一堆代码,通过一组数据,然后find数据进入下一个值步骤(5单位增量)的行。 当find这个位置时,插入5行,在这些行中,STDEVP,MIN,MAX和AVERAGE在该数据块的范围内计算。 input后,循环继续遍历数据,直到碰到最后一行(lastRowvariables)。 代码如下所示:

Sub sectionBlocking() Dim lastRow As Integer Dim lastColumn As Integer Dim sectionLastRow As Integer Dim initialValue As Double Dim cellDifference As Double Dim stepSize As Integer Dim firstCell As Integer Dim lastCell As Integer firstCell = 2 lastCell = 2 initialValue = 84 stepSize = 5 lastRow = Range("A1").End(xlDown).Row lastColumn = Range("A1").End(xlToRight).Column For x = 2 To lastRow cellDifference = Range("B" & (x)).Value - initialValue If Abs(cellDifference) > 1 Then lastCell = x - 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "StdDev" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=STDEVP(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "MIN" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=MIN(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "MAX" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=MAX(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "AVG" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=AVERAGE(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert x = x + 1 firstCell = x initialValue = initialValue + stepSize 'lastRow = lastRow + 5 End If Next x lastCell = x - 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "StdDev" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=STDEVP(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "MIN" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=MIN(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "MAX" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=MAX(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y x = x + 1 Range("B" & (x)).EntireRow.Insert Range("A" & (x)) = "AVG" For y = 2 To lastColumn Cells(x, y).FormulaR1C1 = "=AVERAGE(" & "R" & firstCell & "C" & y & ": R" & lastCell & "C" & y & ")" Next y 'lastRow = lastRow + 4 End Sub 

这工作完美,直到我试图执行最后24行的代码才发现macros已插入在最后一块数据的中间,而不是结束的新行,如预期的那样。 我经历了debugging,意识到For循环中的行值variables“x”在原始的“lastRow”位置停止,而新的行被input到图表之后不再是新的位置。 这导致我尝试把下面的代码行(在上面注释,以显示我自己debugging的进度):

 lastRow = lastRow + 5 

这被放在For循环中,使得“lastRow”的值增加了每增加一行就添加的行数。 不幸的是,这也不起作用。 “lastRow”variables正在增加它的值(通过逐步遍历macros),但是For循环仍然在没有该行的地方结束。

我的TL; DR版本的问题是:是否有一种方法来增加For循环的长度,而你已经在循环内部,或者有一个更好的方法来执行相同的操作?

如果解释混乱,我可以提供一些数据。 感谢您的时间。

我做了一个简单的testing,发现了同样的问题:

 Sub Macro1() Dim x As Integer: x = 10 For i = 1 To x If (i = 5) Then x = 15 End If Next MsgBox (i) End Sub 

看起来像excel预编译for循环的限制。 你可以改变你的循环不是。

看到这个职位

 x = 2 While x <= lastRow cellDifference = Range("B" & (x)).Value - initialValue If Abs(cellDifference) > 1 Then 'your code lastRow = lastRow + 1 End If x = x + 1 Wend 

我会尝试Do_While_Loop

 Do While x <= lastRow 'Code that triggers insert lastRow = lastRow + 5 '..... x = x + 1 Loop 

我认为这应该工作,如果你改变你陈述

 for x = 2 to Range("A1").End(xlDown).Row 

这样,你总是使用最后一行。 此刻,您将“旧”最后一行保存在您的variableslastRow中 ,这会使您的for-loop“非dynamic”。

你需要为你的列循环做同样的事情…