+ Reply to Thread
Results 1 to 10 of 10

Union of Named Ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Union of Named Ranges

    Having some issues. Simply want to combine 2 named ranges into one on a cell change event. Cell change event is no problem. I can't get the union to work. I have this code on sheet 3. What am I doing wrong?
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)   'Do nothing if more than one cell is changed or content deleted
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        If Target.Address = "$D$1" Then
            Dim std, usr, all As Excel.Range
            With ActiveWorkbook
                Set std = Names("stdprojects").RefersToRange
                Set usr = Names("users").RefersToRange
                Set all = Union([std], [usr])
            End With
            ActiveWorkbook.Names.Add Name:="AllEvent", RefersTo:=all
       End If
    End Sub

  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: Union of Named Ranges

    Maybe ...

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$D$1" And _
           Not IsEmpty(Target.Value) Then
            Me.Parent.Names.Add Name:="AllEvent", _
                                RefersTo:=Range("stdprojects, users")
        End If
    End Sub
    Last edited by shg; 11-08-2012 at 11:56 AM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Union of Named Ranges

    Getting a 1004 error. "Method 'Range' of object '_Worksheet' failed" on this line
    Me.Parent.Names.Add Name:="AllEvent", _
                                RefersTo:=Range("stdprojects, users")
    Should I have this in a worksheet or in a seperate Module?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Remove the square brackets.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    11-08-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Union of Named Ranges

    Removing the square brackets didn't work. I had it like that before, but I thought I would try them.

    Any ideas why your code won't work in my spreadsheet shg?

    Any one else worked on something like this in the past? Is there an easier way to get 2 dynamic ranges into a drop down validation cell? I'm just sitting scratching my head now.

  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

    Re: Union of Named Ranges

    Post a workbook.

  7. #7
    Registered User
    Join Date
    11-08-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Union of Named Ranges

    My actual workbook I won't post here as it is becoming huge and I don't want to confuse anyone. Here is basically what I'm trying to do. I used the same code and getting the same errors.

    Thanks for the help everyone

    union trial.xlsm

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Union of Named Ranges

    You could use
    ActiveWorkbook.Names.Add Name:="AllEvent", RefersTo:= "=" & all.Address(,,,True)
    or use Excel's native union operator (the comma) in the defintion of AllEvent
    Private Sub Worksheet_Change(ByVal Target As Range)
       'Do nothing if more than one cell is changed or content deleted
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        If Target.Address = "$D$1" Then
            ActiveWorkbook.Names.Add Name:="AllEvent", RefersTo:= "=stdprojects,users"
       End If
    End Sub
    This approach might remove the need for a change event.
    Last edited by mikerickson; 11-12-2012 at 12:32 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Union of Named Ranges

    But DV will not work with a non contiguous range.

    You would need to construct a range of contiguous information.
    See DV0014 - Combine Multiple Lists into One
    http://www.contextures.com/excelfiles.html
    Cheers
    Andy
    www.andypope.info

  10. #10
    Registered User
    Join Date
    11-08-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Union of Named Ranges

    mikerickson, having trouble using the resulting named range in a data validation cell as that has a maximum of 1 column allowed. Hence why I was attempted to use the union. But I didn't fully understand the way the named ranges worked until now.

    *face palm* Of course. Thank you Andy. I'll rework this sheet to create a combined list on a hidden sheet and feed the data validation from there. Much easier solution.

+ 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