+ Reply to Thread
Results 1 to 13 of 13

Named Ranges In Custom Functions

  1. #1
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36

    Named Ranges In Custom Functions

    I have created an add-in with custom functions that are dependent upon named ranges that I have defined in thespreadsheet part of the Excel Add-In File. When using these functions in a new workbook, the ranges are not available. Is there a better way to save named ranges so that they work as part of an add-in or do I have to accomplish this through VBA code?

    Thanks

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Not a good idea to do it that way. Add the Named Ranges using code
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You will need to reference the named ranges using the ThisWorkbook object.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    If you are indeed referring to UDFs (versus Subs), it's very bad practice to refer to a named range. For one thing, the UDF will not recalculate if the range changes.

    UDFs should receive all arguments necessary to return a result.

    =myFunc(myRange) is fine if myRange is a named range, because it gets dereferenced to the actual range when passed (whether static or dynamic), and is appropriately dependent on the range's values.

    =myFunc() is bad if myFunc refers to any range internally.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36
    Thanks for the response guys, it's greatly appreciated.

    I'm not that familiar with VBA coding for named ranges. Let's say I have a named range from A1:B60 in Sheet1. How would I add the named ranges using code?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36
    If I was to save this code in an Add-In file, would the range "Bob" then be available to anyone adding this Add-In?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Bob's your uncle.

  9. #9
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36
    The more I read these responses, the more it makes me think I am looking at this the wrong way. Let me try this again. In my custom functions one of the variables I read in is AGE. There are numerous AGE based 2 column static tables that I would like to effectively vlookup from. I would like the function users to be able to pass in the name of a Table (ie : TABLE1, TABLE2, . . ., TABLE50) and each table has over 100 entries. I was hoping to come up with a suitable solution without having to manually type in each item. I currently have "TABLE1", "TABLE2", . . . as named ranges in the spreadsheet.

    Thanks again for the responses

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    =myFunc(Table1) is fine

    I was hoping to come up with a suitable solution without having to manually type in each item.
    I don't know what that means.

  11. #11
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36
    My problem is that I can pass "TABLE1" into the function as long as "TABLE1" is defined in the spreadsheet that is open. I would like "TABLE1" to be defined for any spreadsheet that is opened.

    I am trying to avoid
    Please Login or Register  to view this content.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    How about backing up a few steps and explaining the issue?

  13. #13
    Registered User
    Join Date
    11-06-2008
    Location
    Canada
    Posts
    36
    I am writing a custom function that reads in variables for Age as well as an Age based lookup table. Each table has a corresponding value for each age (from 0 to 120) and there are at least a dozen different tables to choose from.

    In the spreadsheet that I am saving as an Add-In, I named the range Sheet1!$A$1:$B$120 to be "TABLE1" hoping that when I passed "TABLE1" into the function from any other spreadsheet it would take the values from [AddIn.xls]Sheet1!$A$1:$B$120. Is there some other way to do this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1