我想找出列中每一行之间的差异并将其存储为变量,然后如果值 > 1 则插入那么多空白行
I want to find the difference between each row in a column and store that as a variable and then insert that many blank rows if the value is > 1
以上是我想要的结果。可以看到,当最左边一列的数字从 85 跳到 87,从 89 跳到 91 时,插入了一个空行。如果差异> 1,我希望这种情况发生的次数与行之间的差异一样多。因此,如果最左边的数字从 85 变为 90,它将插入 5 个空行。这发生在 D:G.
列之间
这是我在 Whosebug 上从另一个 post 那里获取的代码,但我不确定如何将行的差异存储为变量,然后插入那么多空白行。
ActiveSheet.Cells(4, 2).Activate
While ActiveCell.Value <> ""
If ActiveCell.Value - ActiveCell.Offset(-1, 0).Value < 0 Then
ActiveCell.EntireRow.insert shift:=xlShiftDown
Else
ActiveCell.Offset(1, 0).Activate
End If
Wend
不需要Select/Activate任何东西。并且在插入或删除行时始终向后循环,否则您可能会跳过行。
已修改以适应 C 列。
Sub x()
Dim r As Long, n As Long
For r = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1
n = Cells(r, "C").Value - Cells(r - 1, "C").Value
If n > 1 Then
Cells(r, "C").Resize(n - 1).EntireRow.Insert shift:=xlShiftDown
End If
Next r
End Sub
之前
之后
以上是我想要的结果。可以看到,当最左边一列的数字从 85 跳到 87,从 89 跳到 91 时,插入了一个空行。如果差异> 1,我希望这种情况发生的次数与行之间的差异一样多。因此,如果最左边的数字从 85 变为 90,它将插入 5 个空行。这发生在 D:G.
列之间这是我在 Whosebug 上从另一个 post 那里获取的代码,但我不确定如何将行的差异存储为变量,然后插入那么多空白行。
ActiveSheet.Cells(4, 2).Activate
While ActiveCell.Value <> ""
If ActiveCell.Value - ActiveCell.Offset(-1, 0).Value < 0 Then
ActiveCell.EntireRow.insert shift:=xlShiftDown
Else
ActiveCell.Offset(1, 0).Activate
End If
Wend
不需要Select/Activate任何东西。并且在插入或删除行时始终向后循环,否则您可能会跳过行。
已修改以适应 C 列。
Sub x()
Dim r As Long, n As Long
For r = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1
n = Cells(r, "C").Value - Cells(r - 1, "C").Value
If n > 1 Then
Cells(r, "C").Resize(n - 1).EntireRow.Insert shift:=xlShiftDown
End If
Next r
End Sub
之前
之后