当excel列中的值发生变化时,添加3个空白行



我想在A列的值发生变化时输入3个空白行。我在网上找到了以下vba代码。是否有可能将其调整为3行而不是1行?我还想去掉输入框。我对vba的了解非常有限,所以我为此道歉。

我只需要一些方式来输入3空白行每当在ID列的值没有任何类型的输入框或其他对话框的变化。任何帮助将非常感激!

Dim curR As Range
Set curR = Application.Selection
Set curR = Application.InputBox("Select the Range of Cells to be insert blank rows", xTitleId, curR.Address, Type:=8)
For i = curR.Rows.Count To 2 Step -1
If curR.Cells(i, 1).Value <> curR.Cells(i - 1, 1).Value Then
curR.Cells(i, 1).EntireRow.Insert
End If
Next
End Sub

图片

下面的过程将允许您指定每次更改后要插入多少行。您还可以通过更改常量TargetClm的值来指定另一个ID列。

Sub Insert3Rows()
' 298
Const TargetClm As String = "A"     ' change to suit
Const NumRows   As Integer = 3      ' number of rows to insert
Dim R           As Long             ' loop counter: sheet rows

Application.ScreenUpdating = False
With Worksheets("Sheet1")           ' change to suit
' loop from next to last used cell to row 2
For R = (.Cells(.Rows.Count, TargetClm).End(xlUp).Row - 1) To 2 Step -1
If .Cells(R, TargetClm).Value <> .Cells(R + 1, TargetClm).Value Then
.Cells(R + 1, TargetClm).Resize(NumRows).EntireRow.Insert
End If
Next R
End With
Application.ScreenUpdating = True
End Sub

没有规定处理空白行。因此,请不要在同一工作表上运行代码两次。

最新更新