Excel VBA Search Button

后端 未结 2 926
自闭症患者
自闭症患者 2021-01-21 10:12

I am trying to use a text box and command button to search my entire workbook for a specific word or value. For example, \"3132\" or \"Work Instructions\". So far I am able to

相关标签:
2条回答
  • 2021-01-21 10:49

    You need to loop through the array of worksheet objects in the workbook.worksheets collection.

    0 讨论(0)
  • 2021-01-21 10:57

    Try something like this, which uses FindNext method while looping over the Sheets in the Workbook.

    Sub FindLoopSheets()
    Dim srchString$
    Dim w As Integer
    Dim wsName As String
    Dim rng As Range
    Dim fndRange As Range
    Dim nxtRange As Range
    
    srchString = Application.InputBox("Enter the value to search for", "Search Query")
    
    If srchString = vbNullString Then
        MsgBox "No value entered.", vbInformation
        Exit Sub
    End If
    
    For w = 1 To Sheets.Count
        With Sheets(w)
            wsName = .Name
            Debug.Print "Beginning search on " & wsName
    
            Set rng = .Cells.Find(What:=srchString, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
                xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
                , SearchFormat:=False)
    
            If Not rng Is Nothing Then
                Set fndRange = rng
                Do
                    Set nxtRange = .Cells.FindNext(After:=fndRange)
    
                        Debug.Print Sheets(w).Name & "!" & nxtRange.Address
                        Set fndRange = nxtRange
    
                Loop Until fndRange.Address = rng.Address
    
            Else:
                Debug.Print srchString & " was not found on " & wsName
    
            End If
        End With
    Next w
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题