Searches using a textbox for input

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,373
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Code:
Sub searchvba()

' Find the name in the range A9:A1000
Dim rgFound As Range
Set rgFound = Range("A9:A1000").Find("Bob")
rgFound.Select

End Sub

I have code here that searches in the range for Bob. How do I change it so it searches for the text in a text box instead? The name of the textbox is txtname.

Also, how could I change it again to search for the text in a cell?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this:

VBA Code:
Sub searchvba()

Dim tbText As String
Dim rgFound As Range

tbText = ActiveSheet.txtname.Text
Set rgFound = Range("A1:A1000").Find(tbText)
rgFound.Select

End Sub
 
Upvote 0
Thanks for the reply. I now have this:

VBA Code:
Sub searchvba()
' Find the name in the range A9:A1000
Dim rgFound As Range
Dim tbText As String

tbText = ActiveSheet.TextBox1.Text
Set rgFound = Range("A9:A1000").Find(tbText)
rgFound.Select


End Sub

..but when I try and run it, I get an error: Object variable or with block variable not set.
 
Upvote 0
You'll get that if the string isn't found. Try this to account for that:

Edit: Adjusted to make it partial match.

VBA Code:
Sub Test()

Dim tbText As String
Dim rgFound As Range

tbText = ActiveSheet.TextBox1.Text
Set rgFound = Range("A1:A1000").Find(What:=tbText, LookAt:=xlPart)
If Not rgFound Is Nothing Then
    rgFound.Select
Else
    MsgBox (tbText & " was not found.")
End If
End Sub
 
Upvote 0
Thanks for that code, it works great! It just needs to be able to do partial matches too, such as just a first or last name or even wrong spelling. Could you help me with that, please?
 
Upvote 0
Thanks for that code, it works great! It just needs to be able to do partial matches too, such as just a first or last name or even wrong spelling. Could you help me with that, please?

No problem! Glad to help.

It's not going to cover wrong spelling, but see the edit I made to the previous post. So if you have "Bo", for example, in the textbox, the macro should find "Bob."
 
Upvote 0
That code you edited works great too! Could you add something so that it allows you to cycle through the results if there are more than 1?
 
Upvote 0
I'm sure someone could improve on this, but it should work:

VBA Code:
Option Explicit

Sub Test()

    Dim tbText As String
    Dim rgFound As Range
    Dim firstAddress As String

    tbText = ActiveSheet.TextBox1.Text

    Set rgFound = Range("A1:A1000").Find(What:=tbText, LookAt:=xlPart)
    
    If Not rgFound Is Nothing Then
        firstAddress = rgFound.Address
        Do
            rgFound.Select
            If MsgBox("Go to next match?", vbYesNo) = vbNo Then
                Exit Sub
            End If
            Set rgFound = Range("A1:A1000").Find(What:=tbText, LookAt:=xlPart, After:=rgFound)
        Loop While Not rgFound Is Nothing And rgFound.Address <> firstAddress
        MsgBox "All matches found."
    Else
        MsgBox tbText & " was not found."
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,629
Messages
6,173,446
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top