We help IT Professionals succeed at work.
Get Started

Excel VBA: how to change scope of names to local

Last Modified: 2013-11-25
Hi x-perts,

Is there n option to make names local when creating new names as

ThisWorkbook.Worksheets("own").Range("weights")(4, n + 3).Name = "test_approx"

it always crfeates Global names. I have found te code to redefine gobal names to local (see attached, but I would rather try to find a way of creating local names from the beginning (not deleting and recreating them).

Any sggestions?

Sub LocalizeNames()
    Dim nm As Name
    Dim wsh As Worksheet
    Dim sName As String, sRefersto As String
    Set wsh = ThisWorkbook.Worksheets("RM")
    For Each nm In ThisWorkbook.Names
        If nm.Parent.Name = ThisWorkbook.Name Then
            If nm.RefersToRange.Cells(0).Value = nm.Name And _
                nm.RefersToRange.Parent.Name = wsh.Name Then
                sName = nm.Name
                sRefersto = nm.RefersTo
                wsh.Names.Add wsh.Name & "!" & sName, sRefersto
            End If
        End If
    Next nm
End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE