2016-08-05 81 views
0

我发现下面在这里的代码:Insert row below based on cell value excel macro插入行xlUP不工作

它的工作原理,但是,像其他消息中的海报,我要插入现有行下面的新行(这里行一个“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

要插入下面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