Using Find to test if element exist in an Range Array

Erick

Active Member
Joined
Feb 26, 2003
Messages
362
Hi all, I am looking to use the 'Find' vba function to test if an element exist in a range array but I am getting a 'Type mismatch error'.

When I swap my array from a range to a list, it works.

Please help.
VBA Code:
Sub FindBob()
   'Create Array
   Dim a As String
   Dim strName As Variant
   
   strName = Range("A1:A5") 'is generates a Type mismatch error - the range contains the same elements as the list below
   
   'strName = Array("Bob Smith", "John Davies", "Fred Jones", "Steve Jenkins", "Bob Williams") - this method works
   
   'declare a variant to store the filter data in
   Dim strSubNames  As Variant

  'filter the original array
 
   strSubNames = Filter(strName, "Bob")

   'if you UBound value is greater than -1, then the value has been found
   If UBound(strSubNames, 1) > -1 Then MsgBox ("I found Bob")
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Change
VBA Code:
strName = Range("A1:A5")
to
VBA Code:
strName = Application.Transpose(Range("A1:A5"))

Also be aware that if Bob Smith and Bob Williams did not appear in the list but Bobette Jones did, the message box would still say "I found Bob"
 
Upvote 0
Thanks Peter and Joe. I have a large data, which of the 2 options (match or filter) is more efficient?
Also, I tried the match option as you suggested, with the line of code below but I am getting an Error 2042, which I read was effectively an N/A result.

"If Not IsError(Application.Match("Bob", strName, 0)) Then MsgBox ("I found Bob again")"
 
Upvote 0
Thanks Peter and Joe. I have a large data, which of the 2 options (match or filter) is more efficient?
Also, I tried the match option as you suggested, with the line of code below but I am getting an Error 2042, which I read was effectively an N/A result.

"If Not IsError(Application.Match("Bob", strName, 0)) Then MsgBox ("I found Bob again")"
That is not quite the way they show in the code. They show a format of:
VBA Code:
If IsNumeric(Application.Match("Bob", strName, 0)) Then MsgBox ("I found Bob again")"
and that is assuming that strName is an array, and not a range.
 
Upvote 0
Can you confirm whether or not you want to show "Found" if the text being searched for is a sub-string of a longer text as in the example at the bottom of my previous post?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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