Match TextBox Value to values in Range

GuyGadois

Active Member
Joined
Jan 12, 2009
Messages
344
Office Version
  1. 2019
Platform
  1. Windows
How would I match a textbox value to any value in a range.

When a user enters in a value in Textbox1 the VBA compares it to a list of numbers to see if it matches. If it does then the sub continues, if not the sub ends. I am able to match it to one cell but not able to get it to work in a a range.

Thanks,

Guy
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
What shape range?
This would work for a column or row range
Code:
Application.Match(TextBox1.Text, Range("A1:A10"),0)

for a rectangular range, I'd use something like

Code:
Range("A1:D10").Find(What:=TextBox1.Text, After:=ActiveCell, LookIn:=xlFormulas, _
                 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
 
Upvote 0
The shape of the range is one row by 10 columns. Rectangular I guess?

Will that work is a named range instead of specifying cells?

Guy
 
Upvote 0
What happened when you tried it?


Error with naming cells
Code:
If Worksheets("Admin").Range("f114:f124").Find(What:=TextBox1.Text, After:=ActiveCell, LookIn:=xlFormulas, _
                 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) Then

Run-time error '13':

Type Mismatch


Error with name range:
Code:
If Worksheets("Admin").Range("Admin_password_range").Find(What:=TextBox1.Text, After:=ActiveCell, LookIn:=xlFormulas, _
                 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) Then
Run-time error '91':
object variable or with block variable not set
 
Upvote 0
Worksheet references aren't needed if the Name is scoped to the Workbook level.
This worked for me:
Code:
MsgBox Application.Match("Test String", Range("myName"), 0)

The mismatch in the second bit of code is that the function .Find returns a Range object and If needs a Boolean condition.
 
Upvote 0
Worksheet references aren't needed if the Name is scoped to the Workbook level.
This worked for me:
Code:
MsgBox Application.Match("Test String", Range("myName"), 0)
The mismatch in the second bit of code is that the function .Find returns a Range object and If needs a Boolean condition.

I am afraid that is is probably over my head. I tried replacing the .find to a boolean but that did not work. Then I tried the code:

Code:
MsgBox Application.Match("Test String", Range("myName"), 0)</pre>

But couldn't figure out how to use it. I searched a few places on the internet but couldn't find a reasonable explanation what is expected when using a Match function. I have a simple code to work to match one cell:

Code:
If TextBox1.Text = Range("name") Then

but, when the range "Name" is a range of multiple cells (one column x 10 rows), it doesn't work.

Guy
 
Upvote 0
Code:
If IsNumeric (Application.Match(TextBox1.Text, Range("name"),0)) Then
    MsgBox "Its in the Range"
Else
    MsgBox "its not"
End If
Code:
Dim aRange as Range

On Error Resume Next
    Set aRange = Range("name").Find(what:=TextBox1.Text)
On Error GoTo 0

If aRange Is Nothing Then
    MsgBox "not there"
Else
    MsgBox "Its there"
End If
 
Upvote 0
Code:
If IsNumeric (Application.Match(TextBox1.Text, Range("name"),0)) Then
    MsgBox "Its in the Range"
Else
    MsgBox "its not"
End If
Code:
Dim aRange as Range

On Error Resume Next
    Set aRange = Range("name").Find(what:=TextBox1.Text)
On Error GoTo 0

If aRange Is Nothing Then
    MsgBox "not there"
Else
    MsgBox "Its there"
End If


OK, the second makes more sense to me. Thanks I will firgure out how to apply that one. Thank you very much!

Guy
 
Upvote 0

Forum statistics

Threads
1,222,608
Messages
6,167,042
Members
452,093
Latest member
JamesFromAustin

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