2013-05-07 127 views
2

我正在嘗試使用文本框和命令按鈕來搜索特定單詞或值的整個工作簿。例如,「3132」或「工作說明」。到目前爲止,我可以搜索我正在使用的工作表,但無法搜索工作簿的其餘部分。另外,一些工作表是隱藏的。任何洞察這將是有益的,並幫助我出一噸!我在下面列出了我的正確程序:Excel VBA搜索按鈕

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 

有一個好的!

+0

下面是通過每個工作循環搜索整個工作簿的例子:http://excelvbaandmacros.blogspot.com/2011/09/search-stringvalue-in-entire-workbook.html – PowerUser 2013-05-07 15:34:18

回答

3

您需要循環訪問workbook.worksheets集合中的工作表對象數組。

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 
+0

+ 1換去額外的一英里。 – 2013-05-07 22:35:14