gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:00:10 26 4
gpt4 key购买 nike

我有一堆代码,它们遍历一组数据,然后找到数据进入下一个值步骤(5 个单位增量)的行。找到此位置后,将插入 5 行,并在这些行中计算该数据 block 范围内的 STDEVP、MIN、MAX 和 AVERAGE。输入后,循环继续遍历数据,直到到达最后一行(lastRow 变量)。其代码如下所示:

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 行代码,却发现宏已将新行插入到最后一个数据 block 的中间,而不是按照预期插入到末尾。我进行了调试,发现 For 循环中的行值变量“x”停止在原始“lastRow”位置,而不是新行输入图表后的新位置。这导致我尝试放入下面的代码行(上面注释以显示我自己调试的进度):

lastRow = lastRow + 5

将其放入 For 循环中,以便每次添加行时,“lastRow”值都会增加添加的行数。不幸的是,这也不起作用。 “lastRow”变量正在增加其值(通过逐步执行宏发现),但 For 循环仍然在没有该行的同一位置结束。

我的问题的 TL;DR 版本是:当您已经在循环内部时,是否有办法增加 For 循环的长度,或者是否有更优雅的方法来执行相同的操作?

如果解释令人困惑,我可以提供一些数据。感谢您抽出时间。

最佳答案

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

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 循环的限制。您可以将循环更改为 while

查看此post

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

关于vba - 在循环中更改 For 循环的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com