+ Reply to Thread
Results 1 to 3 of 3

Automatically Assign a Named Range Based on a Field

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2014
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    3

    Automatically Assign a Named Range Based on a Field

    Hi.

    I have 2 fields. Column A is a bunch of values (not important). Column B contains the name of the named ranges I want to assign to the values in column A. For example:

    Column A Column B
    ABC123 namedRange1
    ABC456 namedRange1
    ABC789 namedRange2
    DEF123 namedRange3
    DEF456 namedRange3
    DEF789 namedRange3

    In the example above, I want values 'ABC123' & 'ABC456' to be in a named range called 'namedRange1'. Now I know how to do this by simply highlighting the values & typing the named range in the name box but seeing as there are hundreds of these, I would like to know if it is possible to automate this via vba or a function?

    Regards.
    Erik.

  2. #2
    Forum Contributor
    Join Date
    09-05-2012
    Location
    Dubai
    MS-Off Ver
    Office 365
    Posts
    409

    Re: Automatically Assign a Named Range Based on a Field

    Hi.
    Above is user defined .. like Range 1 you have taken 123 & 456 of ABC but for Range3 you took 3 rows which is 123 , 456 ,789 of DEF.. and only 1 for Range 2.....

    do you follow the same pattern for rest of your data ???

  3. #3
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,446

    Re: Automatically Assign a Named Range Based on a Field

    Hi,

    Does this help?

    Sub a()
    Dim rCell As Range, rNamedRange As Range
    For Each rCell In Range("B1:B6")
    If rCell.Value = "namedRange1" Then Set rNamedRange = ModUnion(rNamedRange, rCell.Offset(0, -1))
    Next
    
    ThisWorkbook.Names.Add Name:="namedRange1", RefersTo:=rNamedRange
    
    
    End Sub
    
    Function ModUnion(Rng1 As Range, Rng2 As Range) As Range
        If Rng1 Is Nothing Then
            Set ModUnion = Rng2
        ElseIf Rng2 Is Nothing Then
            Set ModUnion = Rng1
        Else
            Set ModUnion = Application.Union(Rng1, Rng2)
            End If
        End Function
    Note the use of the modified Union function - the native function doesn't like having an argument that "Is Nothing"
    Last edited by sweep; 11-13-2014 at 04:13 AM.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Automatically answer Named Conflict Dialog with No and set Named range to variable
    By mlj61289 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2013, 08:32 PM
  2. SUM columns based on a named range (with more than one field) and a row ID
    By aaronnoparstak in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-13-2012, 07:02 PM
  3. Assign named range in worksheet to combo box
    By AppSupportKarl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2011, 06:38 AM
  4. assign name based another name field
    By D_West in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2009, 12:00 PM
  5. [SOLVED] Dynamically assign ComboBox.List from named range areas
    By Paul Martin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2005, 01:05 AM

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