UDF if formula that contains specific word

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
I have a UDF which basically accomplishes a lookup. To do that I use this code:
Code:
Function name(number As String)
Select Case number
Case Is = "1"
name = "BOB"
.
.
.
there are a bunch of cases as well.

I'm not too familiar with UDFs so I am trying to figure out how to have one that does the same thing, but if it contains a certain word. Is it possible to do this with a case format since I have found that to be the fastest (computer processing speed)? Something like...
Code:
Function Number (Textname as string)
Select Case Textname
Case Contains = "BOB"
or 
Case is = "*BOB*"

I could probably do this with a long list of if statements, but I think that would run too slowly if I had to use it for a sheet with a lot of rows (50k+).

Thanks for any insight!
 
Just make the name lower (or upper) case before doing the checks, then make your check string the same way:

Code:
Function testfunc(name As String) As String
    Dim checkName As String
 
    checkname = LCase(name)        ' The name goes to lower

    Select Case True
    Case checkname Like "*bob*"     ' All match strings go to lower
        testfunc = "1"
    Case checkname Like "sam*"
        testfunc = "2"
    Case checkname = "harry"
        testfunc = "3"
    End Select
    
End Function

HTH
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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