整个工作簿中的InputBox的FindNext

在这里吃饭。 我在这个网站上发现了很多代码,并且要感谢所有贡献者。

我的问题是我有一个用户窗体。 我点击一个button来调出一个InputBox,在那里input一个值来search银行名称,银行家名字,公司名称等。

我有代码来执行search没有问题,但我希望能够继续search所有InputBox值的实例。 例如,search名称“史密斯”,如果第一个不是我需要的,继续search,直到我登陆在我正在寻找的那个。

Dim ws As Worksheet Dim rFound As Range Dim strName As String On Error Resume Next strName = InputBox("Please Enter Search Value." & vbNewLine & "Entry Must Be Exact Cell Value!", "Search Value") If strName = "" Then Exit Sub For Each ws In Worksheets With ws.UsedRange Set rFound = .Find(What:=strName, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart) If Not rFound Is Nothing Then firstaddress = rFound.Address Application.Goto rFound, True Exit Sub End If End With Next ws On Error GoTo 0 MsgBox "Merchant not found. Please make sure you typed it correctly.", vbOKOnly + vbCritical, "Invalid Entry" 

VBA已经有一个.FindNext()方法用于这个目的:

 Sub SO() Dim inputString As String Dim foundCell As Excel.Range Dim wSheet As Excel.Worksheet Dim foundAddress As String inputString = InputBox("Please enter search term:", "Search") For Each wSheet In ActiveWorkbook.Worksheets Set foundCell = wSheet.Cells.Find(inputString, , -4163, 2) If Not foundCell Is Nothing Then Application.Goto foundCell, True foundAddress = foundCell.Address If MsgBox("Match in " & wSheet.Name & " (" & foundCell.Address & ")" & vbCrLf & "Continue?", 68, "Match Found") = vbYes Then Do Set foundCell = wSheet.Cells.FindNext(foundCell) If Not foundCell Is Nothing And Not foundCell.Address = foundAddress Then Application.Goto foundCell, True Else Exit Do End If Loop While MsgBox("Match in " & wSheet.Name & " (" & foundCell.Address & ")" & vbCrLf & "Continue?", 68, "Match Found") = vbYes And _ Not foundCell Is Nothing And Not foundCell.Address = foundAddress Set foundCell = wSheet.Cells.FindNext(foundCell) End If End If If MsgBox("All matches in this sheet found - move to next sheet?", 68, "Next Sheet?") = vbNo Then Exit Sub Next wSheet End Sub 

您需要修改您的search,以便您的代码能够“记住”它停留的位置,如下所示:

 Option Explicit Dim ws As Worksheet Dim rFound As Range Dim strName As String Static First as Range 'On Error Resume Next if First is Nothing Then 'we haven't found anything yet Set First = Worksheets(1).Cells(1,1) 'start searching at the beginning End If strName = InputBox("Please Enter Search Value." & vbNewLine & "Entry Must Be Exact Cell Value!", "Search Value") If strName = "" Then Exit Sub For Each ws In Worksheets With ws.UsedRange Set rFound = .Find(What:=strName, After:=First, LookIn:=xlValues, LookAt:=xlPart) while Not rFound Is Nothing if first is nothing then First = rFound 'store off this address for use in our next search end if if first <> rFound Then 'we've found a NEW instance of the search item firstaddress = rFound.Address Application.Goto rFound, True MsgBox "Found one!" Set rFound = .Find(What:=strName, After:=rFound, LookIn:=xlValues, LookAt:=xlPart) else 'we're back at the start, so jump out of the loop set rFound = Nothing End If wEnd End With Next ws On Error GoTo 0 MsgBox "Merchant not found. Please make sure you typed it correctly.", vbOKOnly + vbCritical, "Invalid Entry" 

几点:

  • 我添加了Option Explicit ,这意味着你的代码现在不会运行,因为你从来没有声明firstaddress 。 从function区启用该选项对于您的理智至关重要:工具| 选项| 然后编辑器检查Require Variable Declaration
  • 通过将First声明为Static ,它将在您的search例程的调用之间保持设置。 这样,因为我们将First放到.Find()函数中,所以它会从中断的地方继续search。
  • 如果您需要再次从头开始search,则可以存储“最后一个”search项 – 如果当前项与上一项不同,请重置set First = Worksheets(1).Cells(1,1)
  • 附加说明 – On Error Resume Next非常有限的情况On Error Resume Next是有用的。 这不是其中之一。 它允许您忽略代码中的错误,以便您可以立即处理它,而这种情况并不是您想要的。 后续的On Error Goto 0 ,重新启用默认的error handling,应该永远不会超过1行代码 – 不是一个完整的子程序。