我想包含指定文字的两个单元之间删除所有行。
例如:细胞B16
包含Description
和细胞B28
包含Transportation
。我想删除含有Description
和Transportation
细胞的行之间的所有行。我需要一个VBA解决方案来解决这个问题。
非常感谢提前。 Punith
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
以防止在细胞中寻找什么参数的部件例如Type Description
将不会被发现。
SearchOrder可以安全地被省略,因为我们在一列范围正在寻找。xlNext
。False
。你可以写一个接受扫描的范围内,搜索文本和找到的范围作为参数,并返回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)