Automatically assign Defined Names Based on Another Cell.

ByeBye

New Member
Joined
Feb 9, 2016
Messages
4
John1
John2
Mark1
Mark2
Bill1
Bill2

<tbody>
</tbody>

I'm looking to be able to assign all the John's to a John defined name automatically. I know how to automatically "create from selection" and use the left row but I want all the John's to have the same defined name and all the Marks to have The Mark defined name.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The following macro assumes that the sheet containing the data is the active sheet, and that Columns A and B contain the data. Also, in an effort to ensure that the defined names are named according to the naming convention, the text "rng" is added to the beginning of the name, and any and all spaces are replaced with an underscore.

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] CreateDefinedNames()

    [COLOR=darkblue]Dim[/COLOR] dicNames [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] vKey [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] rData [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] sName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] dicNames = CreateObject("Scripting.Dictionary")
    dicNames.CompareMode = 1
    
    [COLOR=darkblue]Set[/COLOR] rData = Range("A1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] rData.Rows.Count
        sName = rData.Cells(i, 1).Value
        [COLOR=darkblue]If[/COLOR] Len(sName) > 0 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] IsEmpty(dicNames(sName)) [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]Set[/COLOR] dicNames(sName) = rData.Rows(i)
            [COLOR=darkblue]Else[/COLOR]
                [COLOR=darkblue]Set[/COLOR] dicNames(sName) = Union(dicNames(sName), rData.Rows(i))
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] i
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] vKey [COLOR=darkblue]In[/COLOR] dicNames.keys
        ActiveWorkbook.Names.Add Name:="rng" & Replace(vKey, " ", "_"), RefersTo:="=" & dicNames(vKey).Address(, , , [COLOR=darkblue]True[/COLOR])
    [COLOR=darkblue]Next[/COLOR] vKey
    
    MsgBox "Completed . . .", vbInformation
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
In reviewing this, I seem to get the Column A and Column B in the drop down. I've dug through the code but can't seem to find where this portion is nor am I smart enough to tell the code not to do that since I only want column B. Any help would be appreciated.
 
Upvote 0
Try making the following changes in red...

Code:
Option Explicit

Sub CreateDefinedNames()

    Dim dicNames As Object
    Dim vKey As Variant
    Dim rData As Range
    Dim sName As String
    Dim i As Long
    
    Set dicNames = CreateObject("Scripting.Dictionary")
    dicNames.CompareMode = 1
    
    Set rData = Range("A1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    
    For i = 1 To rData.Rows.Count
        sName = rData.Cells(i, 1).Value
        If Len(sName) > 0 Then
            If IsEmpty(dicNames(sName)) Then
                Set dicNames(sName) = rData.[COLOR=#ff0000]Cells(i, 2)[/COLOR]
            Else
                Set dicNames(sName) = Union(dicNames(sName), rData.[COLOR=#ff0000]Cells(i, 2)[/COLOR])
            End If
        End If
    Next i
    
    For Each vKey In dicNames.keys
        ActiveWorkbook.Names.Add Name:="rng" & Replace(vKey, " ", "_"), RefersTo:="=" & dicNames(vKey).Address(, , , True)
    Next vKey
    
    MsgBox "Completed . . .", vbInformation
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top