如何筛选一个过滤的范围,看看是否有任何单元格包含某些文本

我的代码的每个部分到目前为止,我只想编辑一个部分。 我有我的macrossearch过滤范围,看看它是否包含“检入”和“签出”。

不过,我想添加更多的单词来检查。 有什么办法可以改变这个代码,也许使每个单元格search的string数组?

我想我可以添加很多“如果”,但这不是很好玩。

Sub checkk() Dim cl As Range, rng As Range Dim LastRow As Long Dim celltxt As String Dim i As Integer i = 1 With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set rng = Range("A1:A" & LastRow) For Each cl In rng.SpecialCells(xlCellTypeVisible) cl.Select celltxt = ActiveCell.Text If InStr(1, celltxt, "CHECKED OUT") Or InStr(1, celltxt, "CHECKED IN") Then MsgBox ("found it") else MsgBox ("no") End If Next cl If i > 1 Then MsgBox ("Looks like you have to do some more filtering, sort column A by color to see what was tagged") End If End Sub 

当然,你可以创build数组并循环遍历数组

 Sub checkk() Dim cl As Range, rng As Range Dim LastRow As Long Dim celltxt As String Dim arrVarToCheck(2) As Variant arrVarToCheck(0) = "CHECKED OUT" arrVarToCheck(1) = "CHECKED IN" arrVarToCheck(2) = "Foo" With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set rng = Range("A1:A" & LastRow) For Each cl In rng.SpecialCells(xlCellTypeVisible) celltxt = cl.Text For i = 0 To UBound(arrVarToCheck) If InStr(1, celltxt, arrVarToCheck(i)) Then MsgBox ("found it") Else MsgBox ("no") End If Next i Next cl End Sub