Passing Range Into User Defined Functions

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
I have created a user defined function that reads in a named range "Table" that consists of two columns. The first column contains integer ages from 15 to 70 and the second column is the number I need to manipulate. Here is a simplified version of it.

Code:
Function MyFunction(Age Table)

    Dim MyRANGE As Range

    Set MyRANGE = ThisWorkbook.Names(Table).RefersToRange
   
    MyFunction = Application.VLookup(Age, MyRANGE, 2, False)

End Function

I am having speed issues and was wondering if there is a more efficient way to replace the vlookup function.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Is 'Age Table' in the function declaration the same as 'Table' in your Vlookup call?

How Large is the named range 'Table'

How does the speed compare to putting the Vlookup directly in the spreadsheet instead of involving VBA.

Is the purpose of the function my to make it 'easier' to use the vlookup?
 
Upvote 0
Sorry, that was a typo. It should have been

Code:
Function MyFunction(Age, Table)

    Dim MyRANGE As Range

    Set MyRANGE = ThisWorkbook.Names(Table).RefersToRange
   
    MyFunction = Application.VLookup(Age, MyRANGE, 2, False)

End Function

I have created a number of user defined functions that recursively rely on the values calculated in this function. The actual function does more than just the vlookup, but I was wondering if the function itself is the most efficient way to extract data from a named range.
 
Upvote 0
I don't see how a vlookup in a UDF will work faster than it would as a normal worksheet function. Generally Excel's inbuilt formulas are the most efficient.
 
Upvote 0
I'd agree native formulas are generally better, but FWIW, you shoudl really pass the range as a range so that your function updates if you change the range values:
Code:
Function MyFunction(Age, Table As range)

    MyFunction = Application.VLookup(Age, Table, 2, False)

End Function
 
Upvote 0
I don't see how a vlookup in a UDF will work faster than it would as a normal worksheet function. Generally Excel's inbuilt formulas are the most efficient.

I am creating an Excel add-in with named ranges built into the Excel Add-In file and not viewable by the user. This function is then used as a building block for numerous other functions and unfortunately the number of calculations involved cause the functions to be slower than I would like. In looking to streamline the code, I was curious if calling Excel functions in VBasic was the most efficient way to go.
 
Upvote 0
Compared to in a cell, yes, almost certainly.
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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