UDF help!!

Young_Money

New Member
Joined
Jul 8, 2011
Messages
23
Hi guys. I have a code that looks up the client initials and returns the cell to the right of it. However, I want this UDF to ONLY look in one sheet, "Reference_Location" for this information. However, I want the UDF to be able to be run in all worksheets in the excel file, ptwo.xlsm. How do I do this?
I know the Application.Workbooks part is incorrect, but I do not know how to correct it so that I can make it only use that ONE sheet.

Here is my code.

Function Custom_Lookup(ByVal Client_Initials As String) As String
Dim r
Application.Workbooks(“ptwo.xlsm").Worksheets(“Reference_Location”)
For Each r In Range("A:A")
If r = Client_Initials Then
Custom_Lookup = r.Offset(0, 1)
Exit Function
End If
Next

Custom_Lookup = "Not Found"

End Function
 

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
The quick answer to your question...
Code:
For Each r In Application.Workbooks("ptwo.xlsm").Worksheets("Reference_Location").Range("A:A")

You don't need to reference the workbook if the UDF is located within ptwo.xlsm

A couple of comments:
  1. This UDF could be done with just a VLOOKUP formula or an INDEX-MATCH formula.
  2. Also, if you really want to make the UDF, it would be much more efficient to use the .Find method instead of looping through each cell in column A. Column A has over a million cells to check.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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