我在这里找到了以下代码:Insert row below based on cell value excel macro
它有效,但是,就像其他消息中的海报一样,我希望将新行插入现有行(此处为“2”的行),而不是上面。我尝试将Shift:=xlDown
更改为xlUp
,但这没有效果。我错过了什么?
Sub BlankLine()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "C"
StartRow = 1
BlankRows = 1
With ActiveSheet
For R = LastUsedRow() To StartRow + 1 Step -1
If .Cells(R, Col) = "2" Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
End Sub
答案 0 :(得分:0)
要在R
下方插入行,请使用R + 1
。这是你在尝试什么?
Dim R As Long, LastRow As Long
LastRow = LastUsedRow()
With ActiveSheet
For R = LastRow To 2 Step -1
If .Range("C" & R).Value = 2 Then _
.Range("C" & R + 1).EntireRow.Insert Shift:=xlDown
Next R
End With