根据单元格的值在单元格上方添加空行

时间:2016-09-06 08:40:45

标签: excel

在A栏中,我有字符串' new height'在固定间隔的单元格中:A1A5803A11605等等,每5802行。

我想在每个单元格上方添加2391个空行,其中包含字符串' new height'。

1 个答案:

答案 0 :(得分:0)

我在excel-forum上得到了Olly的回答并且有效 -

Sub foo()
    Const lRows As Long = 2391
    Const sText As String = "new height"

    Dim s() As String
    Dim i As Integer

    Application.ScreenUpdating = False

    With ActiveSheet
        .AutoFilterMode = False
        .Range("A:A").AutoFilter field:=1, Criteria1:=sText
        s = Split(.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Address, ",")
        .AutoFilterMode = False
        For i = UBound(s) To LBound(s) Step -1
            .Range(s(i)).Resize(lRows, 1).EntireRow.Insert
        Next i
    End With

    Application.ScreenUpdating = True

End Sub