0

複数の文字列を自動的に検索するにはどうすればよいですか? 文字列の数は可変で、列 A、シート「Plan1」、ワークブック「Book1.xlsm」にあります。検索には Find Method を使用し、入力ボックスを使用して、複数のワークブックのループで文字列を 1 つずつ検索しました。この入力ボックスを文字列のループに置き換えたいと思います。私のコードの一部:

    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 に答える