2017-11-03 50 views
1

我的代码的每个部分都工作到目前为止,我只想编辑一个部分。我有我的宏搜索过滤范围,看看它是否包含“检入”和“签出”。如何筛选过滤范围以查看是否有单元格包含某些文本

但是我想添加更多的单词来检查。有没有什么办法可以改变这个代码,或许可以创建一个字符串数组,用于搜索每个单元格?

我想我可以添加很多“if或者”,但那并不好玩。

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 
+0

[词组中的数量和高亮关键字(可能的重复https://stackoverflow.com/questions/32860792/count-和 - 突出显示关键字内短语)和/或[模式匹配在Excel中](https://stackoverflow.com/questions/32946884/pattern-matching-in-excel)。 – Jeeped

回答

1

是当然的,你可以通过数组来创建数组和循环

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 
+0

太棒了,谢谢! –

相关问题