Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在几个打开的工作簿中查找字符串_Vba_Excel_Excel 2010 - Fatal编程技术网

Vba 在几个打开的工作簿中查找字符串

Vba 在几个打开的工作簿中查找字符串,vba,excel,excel-2010,Vba,Excel,Excel 2010,如何在几个打开的工作簿中查找字符串并为行着色。字符串可以在每张工作表中重复,并且可以出现在A、B或C列中。此代码可以适应更多工作簿吗? 我在一个工作簿中找到以下代码: Sub Search_String() Dim SearchString As String Dim SearchRange As Range, cl As Range Dim Escolhe_Cor As Long Dim FirstFound As String Dim

如何在几个打开的工作簿中查找字符串并为行着色。字符串可以在每张工作表中重复,并且可以出现在A、B或C列中。此代码可以适应更多工作簿吗? 我在一个工作簿中找到以下代码:

        Sub Search_String()

    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim Escolhe_Cor As Long
    Dim FirstFound As String
    Dim sh As Worksheet

    ' Set Search value
    SearchString = InputBox("Digite o número a ser procurado")
    Escolhe_Cor = InputBox("Escolha uma cor para destacar esse número. De 3 a 56")
    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.EntireRow.Font.Bold = True
                cl.EntireRow.Interior.ColorIndex = Escolhe_Cor
                ' 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

无论您提供了什么样的信息,看起来您只需要将当前代码添加到循环中。这将查看每个打开的工作簿,因此如果不想将其应用于所有工作簿,可以使用正则表达式来标识工作簿名称

For Each wb In Workbooks
   'If you only want certain open workbooks searched use this If statement:
   If wb.name = *criteria* Then
      wb.Activate
     'run your code that loops through each sheet
   End If
Next wb

你需要继续你的问题…嗨,这段代码能适应更多的工作簿吗?