Excel VBA UDF

Young_Money

New Member
Joined
Jul 8, 2011
Messages
23
I am trying to make a UDF that prints the text next to the column that is entered.
Here is an example of what my excel sheet looks like
A / B / C
Customer Initials Location
blahblah JJ Virginia
blah GL Texas
hello LO California
ohai FE Georgia
lalala SL New York
adsf JL Washington

I want to be able to type in the initials and get the location returned.
For example, if i type in =getlocation(SL) into any cell, it would return New York
You cannot really see what the sheet looks like here, but it is supposed to be three different columns.
Can anybody help me out with how to do this please?
 
Here it is, and I'll tell you what was wrong with it
Code:
Function Custom_Lookup(ByVal Client_Initials As String) As String
    Dim r'   DON'T dim this as a string (or as anything for that matter)
    For Each r In Range("B:B")
         If r = Client_Initials Then 'you initially wrote "r.string = r"  You never referenced the input
             Custom_Lookup = r.Offset(0, 1) 'don't write ".value" or it messes it up for some reason
             Exit Function
         End If
    Next   '  Don't forget your Next line
     'Here, you had an unneeded "Else"
    Custom_Lookup = "Not Found"
     'Here, you had an unneeded "End If"
End Function

One more thing. When you use this as the formula, don't forget to use quotes.
Code:
=Custom_Lookup("JJ")
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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