Search Across Range works in Excel but not VBA

Marmit424

Board Regular
Joined
Jul 12, 2016
Messages
58
Hey,

Quick background. I was using Match to search for a string across a range. I ran into the 255 character limit on the match function. I came across a post that showed how you could use Search as in the function below to overcome this limit -
Code:
=MATCH(TRUE,INDEX(ISNUMBER(SEARCH(H2,R1:R5)),0),0)

This works wonderfully in Excel. I then replicated this function in VBA and it returns error mismatch on the Search statement.

Code:
LookupWorksheet = "Sheet 2"
LookupValue = "*text here"
LookupRange = "$B$1:$B$5"


Example = WorksheetFunction.Match(True, WorksheetFunction.Index(WorksheetFunction.IsNumber(WorksheetFunction.Search(LookupValue, Worksheets(LookupWorksheet).Range(LookupRange))), 0), 0)

I've found complicated macros that change a simple search function into multi-step For Each Cell in Range statements. Is there a particular reason why Excel can complete a Search across multiple cells while VBA can't? I believe I'm making a simple syntax error or misunderstanding a logical limitation of VBA, perhaps Excel is returning an array through the search and VBA cannot return an array on a search function because it handles arrays differently.

Thanks!
 

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.
You can use the Evaluate method...

Code:
Example = Evaluate("MATCH(TRUE,INDEX(ISNUMBER(SEARCH(""" & LookupValue & """," & LookupRange & ")),0),0)")

Note that the length of the Name used in the Evaluate method is limited to 255 characters. Also, since you're using the SEARCH function, the following would suffice...

Code:
LookupValue = "text here"

The SEARCH function will match the text anywhere with the the string, whether at the beginning, in the middle, or at the end.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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