删除某些包含文本的单元格之间的行-Excel

时间:2018-11-24 22:35:13

标签: vba excel-2010

我对任何一种编程方法都是陌生的,但是我试图删除包含某些文本的两个单元格之间的所有行,然后在电子表格中的所有行(〜13万行)中重复执行此操作。

示例

我要删除单元格B2和B7之间的所有行,基于这些单元格中的文本 Example, I want to remove all rows between cell B2 and B7, based on the text in those cells

非程序员有没有办法做到这一点? :)

2 个答案:

答案 0 :(得分:0)

在此示例中,我假设在B列中没有两个“连续的标准运行->安装运行”,反之亦然。

初始工作表:

enter image description here

最后一页

enter image description here

这是代码:

Sub test()
'remove cells between two cells Standard run->Setup run and Setup run->Standard run

Dim count, i, numCols As Long
Dim startRemove, endRemove, startText, endText As String
Dim rng As Range

startText = "Standard run->Setup run" 'text where we begin to remove the cells
endText = "Setup run->Standard run" ' text where we finish to remove the cells


startRemove = "" 'in this variable we'll put the cell address of Standard run->Setup run Cells(2, 7).Address
endRemove = "" 'in this variable we'll put the cell address of Setup run->Standard run Cells(7, 7).Address

'-----control column B
'count how many rows
numCols = Range("B:B").Cells.SpecialCells(xlCellTypeConstants).count

For i = 1 To numCols

   If Cells(i, 2) = startText Then ' control if current cell has Standard run->Setup run

        startRemove = Cells(i, 2).Address

    ElseIf Cells(i, 2) = endText Then ' control if current cell has Setup run->Standard run

        endRemove = Cells(i, 2).Address

        Range(startRemove & ":" & endRemove).Delete
        startRemove = ""
        endRemove = ""
    End If
Next i
Range("B1:B" & numCols).Cells.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp ' delete empty rows created

End Sub

希望这会有所帮助。

答案 1 :(得分:0)

这是不需要VBA的解决方案

enter image description here

我在C列中插入了一个公式,您可以应用过滤器以删除行。

单元格C2中的公式为

=IF(B2="Standard run->Setup run","start",IF(B2="Setup run->Standard run","end",IF(C1="start","remove",IF(C1="end","do not remove",C1))))