【发布时间】:2021-10-08 00:02:37
【问题描述】:
在工作表中搜索部分字符串的最有效方法是什么?
我正在尝试通过活动工作表的 C 列末尾在 C4 中搜索“N”。如果数组中没有出现“N”,那么我想关闭文件。如果确实出现“N”,我想打印一条消息“找到强制数据”
我知道您可以使用 countif 函数,总结它出现的次数,然后使用 if/then 来确定操作 - 但我想有更好的方法。想法?
我下面的代码在工作表中搜索“N”,然后删除它不存在的行。 注意:我想在搜索中包含 N 之后的 3 个空格
Sub InspectColumn()
'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 C
LastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
' Sets check range as C1 to the last row of C
Set cRange = Range("C1:C" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
' find if 'N ' found in any cell in the row
Res = Evaluate("COUNTIF(" & x & ":" & x & ", ""*N *"")")
' if 'N ' not found delete row
If Res = 0 Then
ActiveSheet.Rows(x).Delete
End If
Next x
结束子
【问题讨论】:
-
使用过滤器并删除?
标签: excel vba string conditional-statements partial