Excel的两个特定细胞之间删除行

问题描述 投票:-1回答:2

我想包含指定文字的两个单元之间删除所有行。

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

非常感谢提前。 Punith

excel vba cell rows
2个回答
2
投票

Delete Rows Between Criterias

  • 更改值的常量部分,以满足您的需求。
  • 第一测试与HideConst cDel As Boolean = False)的代码。当你确定它没有你的愿望,改变cDelTrue删除关键行(Const cDel As Boolean = True)。
  • 含有规定 - (描述,运输工具)的行不会被删除(隐藏)。
  • 如果没有找到任何标准,代码不会做任何事情。

编码

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

查找方法备注

  • 首届说法,什么,包含搜索数据和需要。其他所有参数都是可选的。
  • 的第二参数,之后,被设置为通过省略缺省SearchDirection参数xlNext指示的最后一个单元从第一(上(左))在塔(范围)细胞“在这之后开始搜索”。
  • 第三,第四和第五参数,找钱,注视和SearchOrder,每次都保存,并因此可以在第二搜索(Set Find2 = ...)被省略。 看着被设置为xlValues以防止在式(或意见)搜索。 注视被设定为xlWhole以防止在细胞中寻找什么参数的部件例如Type Description将不会被发现。 SearchOrder可以安全地被省略,因为我们在一列范围正在寻找。
  • 的第六参数,SearchDirection,是由在代码中使用的,因此可以安全地省略缺省xlNext
  • 第七说法,MatchCase,是未在OP的问题解决,因此省略默认False

0
投票

你可以写一个接受扫描的范围内,搜索文本和找到的范围作为参数,并返回True如果找到的范围实际上是找到了一个辅助函数:

Function GetCellWithText(rngToScan As Range, txtToSearch As String, foundRng As Range) As Boolean
    With rngToScan
        Set foundRng = .Find(what:=txtToSearch, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False, after:=.Cells(.Count))
    End With
    GetCellWithText = Not foundRng Is Nothing
End Function

并且按如下方式使用它在你的主代码:

Option Explicit

Sub DeleteRowsBetweenCellsWithSpecificTexts()
    Dim txt1Rng As Range, txt2Rng As Range

    With Range("B1", Cells(Rows.Count, 2).End(xlUp)) ' reference currently active sheet column B range from row 1 down to last not empty one
        If Not GetCellWithText(.Cells, "Description", txt1Rng) Then Exit Sub ' if first text not found do nothing
        If Not GetCellWithText(.Cells, "Transportation", txt1Rng) Then Exit Sub ' if second text not found do nothing

        If txt2Rng.Row = txt1Rng.Row + 1 Then Exit Sub ' if found cells are adjacent then do nothing
    End With

    Range(txt1Rng.Offset(1), txt2Rng.Offset(-1)).Delete
End Sub

此代码作用于当前活动的片

如果你需要在一个特定的表运行它,然后公正和恰当的片规格之前,调用范围(即qazxsw POI)

© www.soinside.com 2019 - 2024. All rights reserved.