在指定行上方插入行 [英] Inserting rows above a specified rows

查看:105
本文介绍了在指定行上方插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在具有订单类型"(如下所述)的指定行上方插入3个额外的行.

I am wondering how to insert 3 extra rows above a specified row that has "order type" (as specified below).

此行在工作表中多次出现.下面的代码有效,除了将行插入到指定行的下方.谢谢

This row occurs multiple times in the sheet. The below code works, except it inserts the rows below the specified row. Thanks

Sub try()
    Dim c As Range

    For Each c In Range("A1:A100")
        If c.Value Like "*Order Type*" Then
            c.Offset(3, 0).EntireRow.Insert
        End If
    Next c
End Sub

推荐答案

如果您的问题是在搜索的条件上方添加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

这篇关于在指定行上方插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆