Accessing Named Range In UDF

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
I have written this function in a macro enabled spreadsheet and it works as expected if I pass in the named range "TESTRANGE" that refers to a range in that spreadsheet. When I save that spreadsheet as an .xlam file and add it as an add-in the function no longer works. Is there some adjustment I need to make in order to access a named range in the .xlam file?

Code:
Function TEST_1(Number, DATA)

    Dim DATA_RANGE As Variant
    
    DATA_RANGE = Range(DATA).Value
          
    TEST_1 = DATA_RANGE(Number, 1)

End Function

Thanks in advance
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
What's the purpose of your function?
ξ
 
Upvote 0
I makes a difference if you want to use the range from the sheet where the UDF is, or some kind of stored or temporarily stored value in the xlam itself. What's DATA supposed to be in this case - is it coming from the sheet where the UDF is, or is it something stored in the xlam itself?
 
Upvote 0
Oops . . . I guess I oversimplified my example. DATA refers to a named range saved inside the xlam file. The xlam file has many different named range arrays containing numbers. I need the user to be able to pass a named range into the UDF for other calculations.

I makes a difference if you want to use the range from the sheet where the UDF is, or some kind of stored or temporarily stored value in the xlam itself. What's DATA supposed to be in this case - is it coming from the sheet where the UDF is, or is it something stored in the xlam itself?
 
Upvote 0
Try:
Code:
Function TEST_1(Number, DATA)

    Dim DATA_RANGE As Variant
    
    DATA_RANGE = [COLOR="Red"]ThisWorkbook.[/COLOR]Range(DATA).Value
          
    TEST_1 = DATA_RANGE(Number, 1)

End Function
 
Upvote 0
That makes sense that you have to tell it where to look for the named range, but I am getting a compile error ("Method or data member not found"). Is it necessary to declare the sheet as well?

Try:
Code:
Function TEST_1(Number, DATA)

    Dim DATA_RANGE As Variant
    
    DATA_RANGE = [COLOR="Red"]ThisWorkbook.[/COLOR]Range(DATA).Value
          
    TEST_1 = DATA_RANGE(Number, 1)

End Function
 
Upvote 0
It looks like you are passing the name as a string and call it with formulas like
=TEST_1(2, "namedRange")

One could declare Data a range value and use formulas like =TEST_1a(2, namedRange)

Code:
Function TEST_1a(Number As Long, DATA As Range) As Variant

    Dim DATA_RANGE As Variant
    
    DATA_RANGE = DATA.Value
          
    TEST_1 = DATA_RANGE(Number, 1)

End Function

If you have to pass DATA as a string,
Code:
Function TEST_1(Number As Long, DATA as String) As Variant

    Dim DATA_RANGE As Variant
    
    With Application.Caller.Parent.Parent
        DATA_RANGE = ThisWorkbook.Names(DATA).RefersToRange.Value
    End With

    TEST_1 = DATA_RANGE(Number, 1)

End Function
 
Upvote 0
If that doesn't work, try:
Code:
Public Function TEST_1(ByVal num As Long, DAT As String)

    TEST_1 = ThisWorkbook.Names(DAT).RefersToRange.Cells(num, 1)

End Function

I've gotten confused again if we're in the xlam or the workbook that's employing the function.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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