excel - 如果单元格包含字符串,Excel VBA删除行
问题描述
我有每周大小不同的数据表。我想删除包含 D 列中字符串“CHF”的行,但同样,D 列的大小每周都会发生变化。我什至不知道从哪里开始。我在写这个问题时看过推荐的类似问题,但仍然没有弄清楚。有什么解决办法吗?
解决方案
删除行(对于...下一个循环壮举。联合)
- 调整常量部分中的值。
Option Explicit
Sub deleteRows()
' Define constants.
Const wsName As String = "Sheet1"
Const cFirst As Long = 2
Const cCol As String = "D"
Const Crit As String = "CHF"
' Define workbook, worksheet, and last row.
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim cLast As Long: cLast = ws.Cells(ws.Rows.Count, cCol).End(xlUp).Row
' Combine cells ('cCell') containing Criteria into Delete Range ('drg').
Dim drg As Range
Dim cCell As Range
Dim i As Long
For i = cFirst To cLast
Set cCell = ws.Cells(i, cCol)
If cCell.Value = Crit Then
If drg Is Nothing Then
Set drg = cCell
Else
Set drg = Union(drg, cCell)
End If
End If
Next i
' Delete entire rows of Delete Range in one go.
If Not drg Is Nothing Then
drg.EntireRow.Delete
End If
End Sub
推荐阅读
- c# - 如何使用异步 GRPC 调用有效地管理内存
- java - 使用 slick util 将文本绘制到屏幕上
- excel - 使用匹配的数字前缀添加新工作表
- c# - 如何在 PDF 文件中显示数字签名
- php - 如何在php(zend框架)中强制下载zip文件
- html - CSS:如何影响:如果最后一个孩子的伪元素之后
- 悬停?
- spartacus-storefront - 标题的 Spartacus 布局无法使其为空 Spartacus 3.2.2
- python - 如何在 Python 中删除部分 PNG 图像以使其不透明
- html - antlr4:如何识别像“aaaaa”bbb“ddddd”这样的字符串
- javascript - 为什么只有 keydown 有效,而 keyup 或 keypress 无效?