如何筛选过滤范围以查看是否有任何单元格包含某些文本

时间:2017-11-03 22:38:22

标签: excel vba

到目前为止,我的代码的每个部分都有效,我只想编辑一个部分。我有一个宏搜索过滤范围,看它是否包含“CHECKED IN”和“CHECKED OUT”。

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

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

                  var q = (from d in db.tblBlogMedias
                         join c in db.tblBlogs on d.BlogId equals x.id
                         select new
                         {
                             d.Id,
                             d.BlogPicturePath
                         }).OrderByDescending(d=>d.Id).Max(d => d.BlogPicturePath);

1 个答案:

答案 0 :(得分: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