首页 > 解决方案 > Excel删除两个特定单元格之间的行

问题描述

我想删除包含特定文本的两个单元格之间的所有行。

例如:单元格B16包含Description和单元格B28包含Transportation。我想删除包含Description和的单元格行之间的所有行Transportation。我需要一个 VBA 解决方案来解决这个问题。

提前非常感谢。普尼斯

标签: excelvbacellrows

解决方案


删除条件之间的行

  • 更改常量部分中的值以满足您的需要。
  • 首先用Hide( Const cDel As Boolean = False) 测试代码。当您确定它符合您的要求时,更改cDelTrue删除关键行 ( Const cDel As Boolean = True)。
  • 包含标准(描述、运输) 的行不会被删除(隐藏)。
  • 如果没有找到任一Criteria,则代码将不执行任何操作。

编码

Sub HideDeleteDT()

    Const cSheet As Variant = "Sheet1"        ' Source Worksheet Name/Index
    Const cStr1 As String = "Description"     ' Criteria 1
    Const cStr2 As String = "Transportation"  ' Criteria 2
    Const cCol As Variant = "B"               ' Criteria Column Letter/Number
    Const cDel As Boolean = False             ' Enable Delete(True), Hide(False)

    Dim Find1 As Range  ' Criteria 1 Cell Range
    Dim Find2 As Range  ' Criteria 2 Cell Range
    Dim LCell As Range  ' Last Cell in Criteria Column

    ' In Source Worksheet
    With ThisWorkbook.Worksheets(cSheet)
        ' In Criteria Column
        With .Columns(cCol)
            ' Assign last cell range in Criteria Column to variable.
            Set LCell = .Cells(.Cells.Count)
            ' Find Criteria 1 and assign the found cell range to variable.
            Set Find1 = .Find(cStr1, LCell, xlValues, xlWhole, xlByColumns)
        End With
        ' Check if Criteria 1 was found.
        If Not Find1 Is Nothing Then
            ' Find Criteria 2 and assign the found cell range to variable.
            Set Find2 = .Range(Find1.Offset(1), LCell).Find(cStr2, LCell)
            ' Check if Criteria 2 was found.
            If Not Find2 Is Nothing Then
                ' To prevent hiding or deleting rows of the Criteria Cell Ranges
                ' after Critical Rows have already been deleted (Delete) or(and)
                ' the Criterias are in concecutive rows (Hide).
                If Find1.Row + 1 < Find2.Row Then
                    ' Hide or delete rows between found Criteria Cell Ranges.
                    If cDel Then ' Delete (Unsafe). You will lose data.
                        .Rows(Find1.Row + 1 & ":" & Find2.Row - 1).Delete
                      Else       ' Hide (Safe). No loss of data.
                        ' Show all rows to visualize what exactly is being
                        ' hidden by the code each time i.e. if rows have
                        ' previously been hidden it would be unclear which ones
                        ' have been hidden each ('this') time.
                        .Rows.Hidden = False
                        .Rows(Find1.Row + 1 & ":" & Find2.Row - 1).Hidden = True
                    End If
                End If
            End If
        End If
    End With

End Sub

查找方法备注

  • 第一个参数What包含要搜索的数据并且是必需的。所有其他参数都是可选的。
  • 第二个参数After设置为从省略的默认 SearchDirection参数指示的列(范围)中的第一个(上(左))单元格开始搜索的最后一个单元格 xlNext
  • 第 3、4 和 5 个参数LookInLookAtSearchOrder每次都会保存,因此可以在第二个 Search ( Set Find2 = ...) 中省略。
    • LookIn设置为xlValues防止在公式(或注释)中搜索。
    • LookAt设置为xlWhole防止在单元格中找到部分What参数,例如Type Description,将找不到。
    • SearchOrder可以安全地省略,因为我们在单列范围内进行搜索。
  • 第 6 个参数SearchDirection默认情况下 xlNext在代码中使用的,因此可以安全地省略。
  • 默认情况下,第 7 个参数MatchCase没有在 OP 的问题中解决,因此被省略。 False

推荐阅读