简体   繁体   English

删除范围之前和之后的所有行

[英]Delete all rows before range and after range

community!社区!

I write the code that finds Range.Address.我编写了查找 Range.Address 的代码。 Between the two ranges is the table I need.这两个范围之间是我需要的表格。 Now my task is: a) Delete all rows before the first range b) delete all rows after the second range The code itself looks like this:现在我的任务是:a) 删除第一个范围之前的所有行 b) 删除第二个范围之后的所有行代码本身如下所示:

Sub FindCellAddressByString()

Dim rngA As Range
Dim rngB As Range
Set rngA = Worksheets(ActiveSheet.Name).Range(Cells.Address).Find("Transit goods to England", lookat:=xlPart)
Set rngB = Worksheets(ActiveSheet.Name).Range(Cells.Address).Find("TOTAL:", lookat:=xlPart)
If Not rngA Is Nothing And Not rngB Is Nothing Then
    MsgBox rngA.Address & " " & rngB.Address
End If

End Sub

Please tell me how do I delete all the rows starting from the startRow and ending with the rngA row?请告诉我如何删除从 startRow 开始到 rngA 行结束的所有行? After that, me need to delete all rows starting from rngB and ending with the final row (endRow).之后,我需要删除从 rngB 开始到最后一行 (endRow) 结束的所有行。 Thanks!谢谢!

Before:前:

在此处输入图像描述

The code:代码:

Sub FindCellAddressByString()
    Dim rngA As Range
    Dim rngB As Range

    Set rngA = Worksheets(ActiveSheet.Name).Range(Cells.Address).Find("Transit goods to England", lookat:=xlPart)
    Set rngB = Worksheets(ActiveSheet.Name).Range(Cells.Address).Find("TOTAL:", lookat:=xlPart)

    If Not rngA Is Nothing And Not rngB Is Nothing Then
        MsgBox rngA.Address & " " & rngB.Address
    End If

    Rows(rngB.Row + 1 & ":" & Rows.Count).Delete
    Rows(1 & ":" & rngA.Row - 1).Delete
End Sub

and after:之后:

在此处输入图像描述

Note the +- in the Delete statements.请注意Delete语句中的+-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM