gpt4 book ai didi

vba - Excel VBA 搜索按钮

转载 作者:行者123 更新时间:2023-12-03 00:29:51 25 4
gpt4 key购买 nike

我正在尝试使用文本框和命令按钮在整个工作簿中搜索特定单词或值。例如,“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

祝你玩得开心!

最佳答案

尝试这样的操作,它在循环工作簿中的工作表时使用 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

关于vba - Excel VBA 搜索按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com