excel - 如果在各种列中找不到部分字符串,则删除行:VBA
问题描述
我正在尝试创建一个宏,该宏将删除该行中任何单元格中不包含部分字符串“.csv”的行。需要注意的是,该字符串不会出现在同一列中。
如果在 B 列中找到“.csv”显式,则以下代码有效:(我需要部分字符串搜索和跨多列)
Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the listed values then...
If .Value <> ".csv" Then
'And .Value <> "PENDING" And .Value <> "CANCELLED" Add to line above if you want to add specifications'
' Delete that row
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
End Sub
解决方案
已发布代码的此改编COUNTIF
用于搜索整行以查找与 的任何部分匹配.csv
,如果找不到,则删除整行。
如果需要,它可以调整为仅删除行的一部分,但需要更多信息。
Option Explicit
Sub DeleteRows()
' Define variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
Dim Res As Variant
' Define LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
' find if '.csv' found in any cell in the row
Res = Evaluate("COUNTIF(" & x & ":" & x & ", ""*.csv*"")")
' if '.csv' not found delete row
If Res = 0 Then
ActiveSheet.Rows(x).Delete
End If
Next x
End Sub
推荐阅读
- reactjs - react-day-picker ref 当前 scrollIntoView 不可访问
- c# - 按钮状态(启用/禁用)取决于 SelectedItem 属性
- reactjs - 反应嵌套在另一个和单击事件中的useRef
- react-native - 在 react-native 中打开时下拉选择器项目不可点击
- python - python中的数据结构-如果我想在python中搜索大量电话号码,哪种数据类型最好来自集合和字典
- php - php 8.0 版上的“mysqli”类,已弃用?
- jupyter-notebook - Jupyterlab:如何删除/隐藏默认的“Python 3”内核?
- python - 何时在 python 中使用元组
- spring-integration - 未从句柄调用 Spring 集成流
- javascript - 为什么我的记忆游戏不起作用?我无法翻牌或开始游戏