Custom Functions

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
I am writing some custom functions to be saved in an Add-In file, but am having trouble saving named ranges. I would like named ranges in the Add-In file to be available in other spreadsheets. Is there a way to do this? Or is there a way to take a range from Excel and save it into a globally available named range using VBA?
 
Assuming that "Table1" is the named range I want passed through the function, I have got the code to work as follows

=MyFunction(Age,"Table1")

How do I get it to work so that it accepts text as well as a text from a cell reference? i.e. :

=MyFunction(Age,A1)

if cell A1 contains the text "Table1"
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
It should do that by default. Is it not working? Are you getting an error?
 
Upvote 0
In the function dialog box, next to the Table variable that includes the reference to cell A1 the value is showing up as "Table1", but it is returning a "#VALUE" error.
 
Upvote 0
What is the code you are using? Have you tried stepping through it to see where the error is occurring? (put a breakpoint in and then enter the function in a cell)
 
Upvote 0
Here is the code

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 tried the breakpoint but I couldn't figure out much other than it doesn't seem to like the named range coming through a cell reference.
 
Upvote 0
Any thoughts??? I am still unable to make my function work with the cell reference to A1 when A1 contains the text "Table1"
 
Upvote 0
As I understand it, the text "Table" is in A1 and you want the UDF myFunction to act reference that range in the formula =MYFUNCTION(12,A1)
You could try specifying the data type
Code:
Function myFunction(Age as Double, Table as String) as Variant
 
Upvote 0
While this fixes the problem, it causes my "IsMissing" tests for my optional variables to stop working. Is there any way around this?
 
Upvote 0
What optional variables? There aren't any in the code you posted.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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