excel - Excel删除两个特定单元格之间的行
问题描述
我想删除包含特定文本的两个单元格之间的所有行。
例如:单元格B16
包含Description
和单元格B28
包含Transportation
。我想删除包含Description
和的单元格行之间的所有行Transportation
。我需要一个 VBA 解决方案来解决这个问题。
提前非常感谢。普尼斯
解决方案
删除条件之间的行
- 更改常量部分中的值以满足您的需要。
- 首先用
Hide
(Const cDel As Boolean = False
) 测试代码。当您确定它符合您的要求时,更改cDel
为True
删除关键行 (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 个参数LookIn、LookAt和
SearchOrder每次都会保存,因此可以在第二个 Search (
Set Find2 = ...
) 中省略。- LookIn设置为
xlValues
防止在公式(或注释)中搜索。 - LookAt设置为
xlWhole
防止在单元格中找到部分What参数,例如Type Description
,将找不到。 - SearchOrder可以安全地省略,因为我们在单列范围内进行搜索。
- LookIn设置为
- 第 6 个参数SearchDirection是默认情况下
xlNext
在代码中使用的,因此可以安全地省略。 - 默认情况下,第 7 个参数MatchCase没有在 OP 的问题中解决,因此被省略。
False
推荐阅读
- java - 为什么我不能在这里打印我的声明或任何东西?
- pandas - 如何从 xarray 中找到 pandas 数据框中的索引?
- mysql - mysqli按多列计数组
- firebase - 如何为 Firebase 函数 + VueJS 项目组织文件夹结构?
- redis - 无法连接到 MemoryStore 实例(GKE、Node.js)
- python - 两个“np.longdouble”之和产生与打印无关的数字错误
- c# - C# 仅在 foreach 循环中获取第一个 html 节点
- ethereum - 向以太坊智能合约发送数据,交易未被验证(区块未形成)
- sql - 加入两个包含分组和子查询的查询
- cygwin - ./autogen.sh:第 28 行:找不到命令 #139