+ Reply to Thread
Results 1 to 12 of 12

creating a named range and referring to its cells

Hybrid View

  1. #1
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90

    creating a named range and referring to its cells

    here is the snippet of code I'm using

        'add a named range ("output_response_sheet_dv_list") to the worksheet
        ActiveWorkbook.Sheets("hidden_named_ranges").Names.Add Name:="output_response_sheet_dv_list", RefersTo:="=OFFSET(hidden_named_ranges!$A$1,1,0,num_design_sheets,1)"
        'fill "output_response_sheet_dv_list" with allowed sheet names
        For i = 1 To num_design_sheets
            ActiveWorkbook.Sheets("hidden_named_ranges").Range("output_response_sheet_dv_list").Cells(i) = allowed_sheet_name(i)
        Next i
    it fails on the red line

    as you can see, according to Excel, the named range exists in the proper sheet. However I'm not able to access (populate) the cells in that range

    I've attached the workbook it it helps
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Looks like num_design_sheets is undefined.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90
    at the time the error is raised, num_design_sheets is defined as type Long, value=2 (it is under the tally_design_sheets breakout in the Locals window)

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    A defined range can't use a VBA variable as part of its definition. It has no visibility.

  5. #5
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90
    ok, that's what I'm seeing. If I hard-code the #2, instead of num_design_sheets, it works.

    How do I workaround this limitation? It needs to be a variable so that the named range will be redefined each time based on the length num_design_sheets

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Create a named constant on the same sheet.

    BTW, if a named range is (correctly) defined, you'll see marching ants around it when you select its 'refers to' in the define name dialog.

    Edit: You realize you only need a named range if formulas refer it it; it can just be a VBA range variable otherwise, right?

    Also, you can index outside a range in VBA.
    Last edited by shg; 12-01-2008 at 12:30 AM.

  7. #7
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90

    data validation: workbook-level vs worksheet-level named ranges

    having trouble getting data validation to work in Excel. I create a named range in one sheet using VBA code, then I want to define a data validation on another sheet that refers to that named range as the List in the Data Validation dialog box.

    The problem is with the named range definition

    when I manually define the range it works fine. It also does not specify which sheet contains this named range.

    but when I use this VBA code to define the range it doesn't work


    ActiveWorkbook.Sheets("hidden_named_ranges").Names.Add Name:="output_response_sheet_dv_list", RefersTo:="=OFFSET(hidden_named_ranges!$C$1,2,0,hidden_named_ranges!$C$2,1)"
    as you can see I fully qualified the named range by declaring that it appears in the sheet "hidden_named_ranges"

    what is the significance of the second column in the dialog box "Define Name"? I thought it meant that the name appears in more than one worksheet, which is not true in my case.

    In order to reference the data validation list to the named range "output_response_sheet_dv_list" which appears on another sheet "hidden_named_ranges", do I need a workbook-level or worksheet-level named range? How do I create that in VBA code?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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