+ Reply to Thread
Results 1 to 6 of 6

Passing Named Range as parameter to Sub()

  1. #1
    Registered User
    Join Date
    04-30-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010
    Posts
    38

    Passing Named Range as parameter to Sub()

    HI,

    I am trying to pass a parameter, a named range on another sheet, to a secondary Sub(). The named range is Slist and it is on a sheet called Setup. In my main Sub() I define

    Dim MenuList As Range
    Set MenuList = ThisWorkbook.Sheets("Setup").Range("SList")

    and call the secondary Sub() as

    Call Add_Drop_Down_Menu_Cell(MenuList)

    My secondary Sub() is
    Please Login or Register  to view this content.
    I am getting application-defined or object defined error 1004. It appears that the Sub cannot locate or access my MenuList. Is there a way I can check that a correct menu list is being passed tot he Sub? Also, any ideas how to fix this? Thank you.
    Last edited by Leith Ross; 05-09-2013 at 09:37 PM. Reason: Added Code Tags

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

    Re: Passing Named Range as parameter to Sub()

    Just guessing (you don't say where the error occurs), but you may want

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    However, the validation range must refer to a range on the worksheet where the validation appears, or to a named range scoped to the sheet with validation that refers to a range on another worksheet.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    04-30-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Passing Named Range as parameter to Sub()

    Thank you shg. The error occurs in the .Add line. I tried both your approaches. The Formula1:="=Slist" approach works except the first entry in the displayed dropdown is blank even though there is no blank in the SList. The other approach with the Formula1:="=" & MenuList.Address does not work. It places a list but the list is not the Slist. In fact most of the list entries are blank except for another text from some other cell. I would prefer to use the Menulist as I use the Sub with different named lists.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Passing Named Range as parameter to Sub()

    perhaps
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Passing Named Range as parameter to Sub()

    Hello Zeos6

    You can easily obtain the formula style range reference from the Named Range by using the Name property. If the range passed is not a Named Range, VBA will throw an error.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    04-30-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Passing Named Range as parameter to Sub()

    Thank you very much Leith Ross. This is EXACTLY what I needed. It works perfectly. Thank you.

+ 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