Insert copied row based on cell value

帅比萌擦擦* 提交于 2020-01-02 23:09:13

问题


The vba code below (modified from here) will insert a blank row above any row that has a value of "0" in column E. Instead of inserting a blank row, is there a way to copy the row with "0" and insert it above itself? Can this vba code be modified to do this?

Sub BlankLine()

    Dim Col As Variant
    Dim BlankRows As Long
    Dim LastRow As Long
    Dim R As Long
    Dim StartRow As Long

        Col = "E"
        StartRow = 1
        BlankRows = 1

            LastRow = Cells(Rows.Count, Col).End(xlUp).Row

            Application.ScreenUpdating = False

            With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) = "0" Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True

End Sub

回答1:


Just add this line

.Cells(R, Col).EntireRow.Copy

before your .Insert line



来源:https://stackoverflow.com/questions/16513654/insert-copied-row-based-on-cell-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!