在VBA中的工作表中搜索字符串

Kir*_*ran 2 excel vba

我不是VBA的专家.但我正在尝试编写一个模块,在工作簿的所有工作表中搜索特定的字符串"ERROR",并使其粗体并为找到的单元格着色.

我能够解析每个工作表.但是,我无法使用FindVBA 的功能.

任何帮助/想法如何进行将帮助我很多.

谢谢

chr*_*sen 8

这是使用Find和格式化找到的单元格的示例

Sub FindERROR()
    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim FirstFound As String
    Dim sh As Worksheet

    ' Set Search value
    SearchString = "ERROR"
    Application.FindFormat.Clear
    ' loop through all sheets
    For Each sh In ActiveWorkbook.Worksheets
        ' Find first instance on sheet
        Set cl = sh.Cells.Find(What:=SearchString, _
            After:=sh.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
        If Not cl Is Nothing Then
            ' if found, remember location
            FirstFound = cl.Address
            ' format found cell
            Do
                cl.Font.Bold = True
                cl.Interior.ColorIndex = 3
                ' find next instance
                Set cl = sh.Cells.FindNext(After:=cl)
                ' repeat until back where we started
            Loop Until FirstFound = cl.Address
        End If
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)