我正在尝试使用文本框和命令按钮在我的整个工作簿中搜索特定的单词或值。例如,“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
有一个好的!
答案 0 :(得分:3)
您需要遍历workbook.worksheets
集合中的工作表对象数组。
答案 1 :(得分: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