我要删除两个包含特定文本的单元格之间的所有行。
例如:单元格B16
包含Description
,单元格B28
包含Transportation
。我想删除包含Description
和Transportation
的单元格行之间的所有行。
我需要一个VBA解决方案来解决这个问题。
非常感谢。 普尼思
答案 0 :(得分:2)
Hide
(Const cDel As Boolean = False
)测试代码。
确定可以满足您的要求后,将cDel
更改为True
即可
删除关键行(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
xlNext
。Set Find2 = ...
)中省略。
xlValues
以防止
搜索公式(或注释)。xlWhole
以防止发现
单元格中 What 参数的一部分,例如找不到Type Description
。xlNext
,其中
在代码中使用,因此可以安全地省略。False
,不是
在OP的问题中解决,因此将其省略。答案 1 :(得分: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
此代码作用于当前活动的工作表
如果您需要在特定工作表上运行它,则只需在Range调用(即Worksheets("MySheetName").Range(...)
)之前先输入适当的工作表规格