0

如何自动搜索多个字符串?字符串的数量是可变的,位于 A 列,工作表“Plan1”,工作簿““Book1.xlsm”。我使用 Find Method 进行搜索,并使用 Input Box 在多个工作簿的循环中一一查找字符串。我想将此输入框替换为通过字符串的循环。我的部分代码:

    Dim wb As Workbook
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim Escolhe_Cor As String
Dim FirstFound As String
Dim ws As Worksheet

str = 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

  SearchString = Trim(str)


 For Each wb In Workbooks
   If wb.Name <> "Book1.xlsm" Then
    wb.Activate

 If Len(SearchString) = "8" Then

    For Each ws In ActiveWorkbook.Worksheets
             ' Find first instance on sheet
    Set cl = ws.Cells.Find(What:=SearchString, _
        After:=ws.Cells(1, 1), _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        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 'etc etc
4

1 回答 1

2

试试下面的代码:

 Dim wb As Workbook
    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim Escolhe_Cor As String
    Dim FirstFound As String
    Dim ws As Worksheet
    Dim searchRng As Range, lastRow As Long, cell As Range


    Dim lastRow As Long
    lastRow = Workbooks("Book1.xlsm").Sheets("Plan1").Range("65000").End(xlUp).Row

    Set searchRng = Workbooks("Book1.xlsm").Sheets("Plan1").Range("A2:A" & lastRow)   '

    For Each cell In searchRng
           SearchString = Trim(cell)

        For Each wb In Workbooks
            If wb.Name <> "Book1.xlsm" Then
                wb.Activate

                If Len(SearchString) = "8" Then

                    For Each ws In ActiveWorkbook.Worksheets
                        ' Find first instance on sheet
                        Set cl = ws.Cells.Find(What:=SearchString, _
                                               After:=ws.Cells(1, 1), _
                                               LookIn:=xlValues, _
                                               LookAt:=xlWhole, _
                                               SearchOrder:=xlByRows, _
                                               SearchDirection:=xlNext, _
                                               MatchCase:=False, _
                                               SearchFormat:=False)
                        If Not cl Is Nothing Then
                            ' if found, remember location
                            FirstFound = cl.Address
于 2013-03-30T02:26:46.413 回答