excel vbasearch范围内的文本,并在文本框中检索结果

这个macros正在工作,但是我想在文本框中显示范围为D2:D82的结果,而不用自动将用户redirect到定位的文本。 当谈到VBA时,我是一个非常基本的用户。我意识到我可能会问一个已经提出的问题,并用不同的线程回答,所以我对此表示歉意。 帮帮我,请改变我的macros?

Sub Button 1_Click() Res = InputBox("Who are you looking for?") Set Rng = Worksheets("Level2").Range("B2:B82") '<< The Sheet Name and Range to Search With Rng Set MyChoice = .Find(What:=Res) If Not MyChoice Is Nothing Then Application.Goto MyChoice Else: GoTo ExitMyChoice End If End With Exit Sub ExitMyChoice: MsgBox "Could Not Find " & Res End Sub 

我不完全确定你想显示什么结果,但是下面的代码可能会有所帮助。 你不需要GoTo语句。 您还应该使用Option Explicit强制声明所有variables,并直接在VBA编辑器中捕获不匹配错误。

 Option Explicit Sub Button1_Click() Dim res As Variant res = InputBox("Who are you looking for?") Dim rng As Range Set rng = Worksheets("Level2").Range("B2:B82") '<< The Sheet Name and Range to Search With rng Dim myChoice As Range Set myChoice = .Find(What:=res) If Not myChoice Is Nothing Then MsgBox "Found '" & res & "' at " & myChoice.Address Else MsgBox "Could not find " & res End If End With End Sub