且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在指定行上方插入行

更新时间:2022-05-31 06:01:15

如果您的问题是在搜索的条件上方添加3行,那么解决问题应该很容易:

If your problem is to add 3 extra rows above searched criteria, solving your problem should be easy:

在您的代码行中:

c.Offset(3, 0).EntireRow.Insert

您的行说找到订单类型后,他应该在下方3行并插入多余的行.

Your line says when order type is found he should go 3 rows below and insert extra row.

应该这样:

c.EntireRow.Resize(3).Insert

我的一行说,找到订单类型后,在其上方添加3行.

My line says, when order type is found, add 3 extra rows above it.

希望这就是您想要的.

我搜索了Internet,发现了与您的问题类似的内容,并根据需要进行了更改.希望这对您有用. 几乎没有解释它是如何工作的:它一直在A列中进行搜索,如果找到了"订单类型",它会在其上方添加3行.当宏转到空白单元格时,它将停止.尝试一下,告诉我它是否对您有用.

I've searched Internet and found something similar to your problem and changed it according to you needs. Hope this will work for you. Little explanation how it works: it searches all the way through A column and if "Order Type" was found it adds 3 rows above it. When macro goes to blank cell it stops. Try it and tell me if it works good for you.

Sub AddRows()

Dim lastValue As String, i As Long, r As Long
Do
    r = r + 1
    If r > 1 And Cells(r, 1).Value Like "*Order Type*" Then
        If Cells(r, 1).Value = "" Then Exit Do
        For i = 1 To 3
            Rows(r).Insert Shift:=xlUp
        Next
        r = r + 3

        Else
            If IsEmpty(Cells(r, 1)) = True Then
            Exit Do

            Else

            End If

    End If
    lastValue = Cells(r, 1).Value
Loop

End Sub