我有大约 100 行和大约 10 列。如果 B(或 2)列中的单元格包含特定字符串“XYZ”(例如),我想删除整行
如果字符串出现在开头,则下面的代码可以完美运行。但我希望它也适用于以下情况:
如果字符串存在,它应该删除整行。
Sub DeleteRowBasedOnCriteria()
Dim RowToTest As Long
Sheets("Jira").Select
For RowToTest = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 2)
If .Value = "XYZ" _
Then _
Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest
End Sub
无论单元格内的位置如何,该函数都应匹配 XYZ 并删除整行。
使用带有通配符的 Like 运算符(也可能带有大小写限定)。
不过,自动筛选可能是更好的选择。
Sub DeleteRowBasedOnCriteria()
Dim RowToTest As Long
Sheets("Jira").Select
For RowToTest = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 2)
If .Value Like "*XYZ*" Then Rows(RowToTest).EntireRow.Delete
'If ucase(.Value) Like "*XYZ*" Then Rows(RowToTest).EntireRow.Delete CASE INSENSITIVE
End With
Next RowToTest
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句