2016-08-05 107 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