Catching error on VBA Match function

a_ud

New Member
Joined
Feb 14, 2012
Messages
27
Hi,

I'm using function Match in VBA (for a fuzzy text search) in the line:

Code:
Application.WorksheetFunction.Match("*" & Trim(Terms(i)) & "*", ActiveSheet.Range("B1:B250"), 0)

Terms(i) is just a string, the function works fine except that eventually it doesn't find any matches. How could I catch this error using IF?

I've tried this:

Code:
If IsError(Application.WorksheetFunction.Match("*" & Trim(Terms(i)) & "*", ActiveSheet.Range("B1:B250"), 0)) Then
            'do something
End If

but again, it throws an execution Debug error. Same for IsNull. IsNA is not available in VBA. Any ideas?

Thanks, a.
 
Except find doesn't always work...
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Rorya,

Curious, what circumstances would it fail? Be interested to know, as I use that code in a lot in much of my procedures.
 
Upvote 0
Well, as written, your code is missing a lot of arguments and since the arguments are retained between Find operations in a session (either in code or in the UI) that is dangerous.
Also, if you specify Lookin:=xlvalues, you won't be able to find in hidden cells.
 
Upvote 0
Thats fine, I'm aware of that. Was on the fly code, all my normal code is fully arguemented :)
 
Upvote 0
There are also some data caused problems ie "" and Null and "*"

Try the code with:

Code:
terms = Array("", "xA", "B", "A", "d", Null, "*")
Good to know about the arguments. I presumed that they were default values, if not set.
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,518
Members
452,650
Latest member
Tinfish

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