2

我正在尝试使用文本框和命令按钮在整个工作簿中搜索特定的单词或值。例如,“3132”或“工作说明”。到目前为止,我可以搜索我所在的工作表,但我无法搜索工作簿的其余部分。另外,一些工作表是隐藏的。对此的任何见解都将是有益的,并且可以帮助我很多!我在下面列出了我的当前程序:

Private Sub CommandButton6_Click()
    Dim strFindWhat As String
    strFindWhat = TextBox1.Text

    On Error GoTo ErrorMessage

    Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Select
    Exit Sub

    ErrorMessage:
        MsgBox ("The data you are searching for does not exist")
End Sub

祝你有个好的一天!

4

2 回答 2

3

您需要遍历workbook.worksheets集合中的工作表对象数组。

于 2013-05-07T15:28:40.607 回答
3

尝试这样的事情,它FindNext在循环工作簿中的表格时使用方法。

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
于 2013-05-07T16:04:49.830 回答