VBA to change the scope of Named Ranges from worksheet level to workbook

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I found this code but it doesn't seem to do the trick. I have named ranges both worksheet and workbook level. I need to change all the worksheet level named ranges to workbook.

Code:
[FONT=Times New Roman][SIZE=3][COLOR=#000000]Public SubRescopeNamedRangesToWorksheet()[/COLOR][/SIZE][/FONT]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim wb As Workbook[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim ws As Worksheet[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim objName As Name[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim sWsName As String[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim sWbName As String[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim sRefersTo As String[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Dim sObjName As String[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Set wb = ActiveWorkbook[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Set ws = ActiveSheet[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    sWsName = ws.Name[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    sWbName = wb.Name[/FONT][/COLOR][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]    [/COLOR][/SIZE][/FONT]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    For Each objName In wb.Names[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]        If objName.Visible = True Then[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]            If InStr(1, objName.RefersTo,sWsName, vbTextCompare) Then[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                sRefersTo = objName.RefersTo[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                sObjName = objName.Name[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                If objName.Parent.Name =sWbName Then[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                    objName.Delete[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                    ws.Names.AddName:=sObjName, RefersTo:=sRefersTo[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]                End If[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]           End If[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]        End If[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Times New Roman]    Next objName[/FONT][/COLOR][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You can adapt this to your need.
Code:
Sub ChangeLocalNameAndOrScope()
'Programmatically change a sheet-level range name and/or scope to a new name and/or scope
Dim nm As Name, Ans As Integer, newNm As String
For Each nm In ActiveWorkbook.Names
    If nm.Name Like "*!*" Then 'It is sheet level
        Ans = MsgBox(nm.Name & " is a worksheet level name - do you want to change it?", vbYesNo)
        If Ans = vbYes Then
            newNm = InputBox("Enter new name - inculde 'Sheet1'! for a local name")
            If newNm = "" Then Exit Sub
            Range(nm.RefersTo).Name = newNm
            nm.Delete
        End If
    End If
Next nm
End Sub
 
Upvote 0
Is there a way to just take the name of the named range as it is now instead of typing it in the InputBox?
 
Upvote 0
Is there a way to just take the name of the named range as it is now instead of typing it in the InputBox?
Sure, try this:
Code:
Sub ChangeLocalNameAndOrScope()
'Programmatically change a sheet-level range name and/or scope to a new name and/or scope
Dim nm As Name, Ans As Integer, newNm As String
For Each nm In ActiveWorkbook.Names
    If nm.Name Like "*!*" Then 'It is sheet level
        newNm = Replace(nm.Name, "*!", "")
        Range(nm.RefersTo).Name = newNm
        nm.Delete
    End If
Next nm
End Sub
 
Upvote 0
Thank you Joe. I can surely work with this.
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,867
Members
449,130
Latest member
lolasmith

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