Hi all - please advise.
How do I delete named ranges with special characters. Or prevent my vba from creating named range with special characters?
My goal is to duplicate a sheet (eg Sheet A) that contains name ranges to another sheet (eg Sheet B). Due to duplicated named ranges - the name ranges on Sheet B are automatically converted to local scope as opposed to global scope. I need a VBA to 1) automatically rename the name ranges on Sheet B so the names don't conflict with the name ranges on original Sheet A and 2) then convert the local scope on Sheet B to global scope.
I've figured out how to perform step 2 thanks to a solution I found on website, but I need help with completing step 1. I'm running into issue where when I rename the named ranges on Sheet B, my VBA automatically adds the sheet name in front of the named range which causes an exclamation point to appear in the new named range. That's an issue because my step 2 requires to delete the current name scoped to worksheet in order to replace with workbook scoped name. But since there's a special (!) character in there, my step 2 VBA can't delete the local scope named ranges.
So how can I rename my local scope named range without the VBA automatically placing the sheet name (that contains the "!" special character) in front of the named range.
Ex/ Cell A1 on Sheet A's named range is "old" I then duplicate this sheet to Sheet B. To rename the named range on A1 Sheet B, I use:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub RangeRename
Dim N As Name
ForEach N In ActiveWorkbook.Names ' I only want to activate current
sheet but not sure how to write that yet
N.Name = WorksheetFunction.Substitute(N.Name,"old","new")' this
replaces range old tonew
Next N
EndSub</code>However, when I run the Macro - the new named range becomes 'SheetB'!new. My step 2 needs to delete this named range but can't! Please help me fix that.
Here's my step 2:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">enter code here
PublicSub RescopeNamedRangesToWorkbook()
Dim wb As Workbook
Dim ws As Worksheet
Dim objName As Name
Dim sWsName AsString
Dim sWbName AsString
Dim sRefersTo AsString
Dim sObjName AsString
Set wb = ActiveWorkbook
Set ws = ActiveSheet
sWsName = ws.Name
sWbName = wb.Name
'Loop through names in worksheet.
ForEach objName In ws.Names
'Check name is visble.
If objName.Visible =TrueThen
'Check name refers to a range on the active sheet.
If InStr(1, objName.RefersTo, sWsName, vbTextCompare)Then
sRefersTo = objName.RefersTo
sObjName = objName.Name
'Check name is scoped to the worksheet.
If objName.Parent.Name <> sWbName Then
'Delete the current name scoped to worksheet replacing with workbook scoped name.
sObjName = Mid(sObjName, InStr(1, sObjName,"!")+1, Len(sObjName))
objName.Delete
wb.Names.Add Name:=sObjName, RefersTo:=sRefersTo
EndIf
EndIf
EndIf
Next objName
EndSub
</code>The above VBA runs into error at objName.Delete because it can't delete special characters.
How do I delete named ranges with special characters. Or prevent my vba from creating named range with special characters?
My goal is to duplicate a sheet (eg Sheet A) that contains name ranges to another sheet (eg Sheet B). Due to duplicated named ranges - the name ranges on Sheet B are automatically converted to local scope as opposed to global scope. I need a VBA to 1) automatically rename the name ranges on Sheet B so the names don't conflict with the name ranges on original Sheet A and 2) then convert the local scope on Sheet B to global scope.
I've figured out how to perform step 2 thanks to a solution I found on website, but I need help with completing step 1. I'm running into issue where when I rename the named ranges on Sheet B, my VBA automatically adds the sheet name in front of the named range which causes an exclamation point to appear in the new named range. That's an issue because my step 2 requires to delete the current name scoped to worksheet in order to replace with workbook scoped name. But since there's a special (!) character in there, my step 2 VBA can't delete the local scope named ranges.
So how can I rename my local scope named range without the VBA automatically placing the sheet name (that contains the "!" special character) in front of the named range.
Ex/ Cell A1 on Sheet A's named range is "old" I then duplicate this sheet to Sheet B. To rename the named range on A1 Sheet B, I use:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub RangeRename
Dim N As Name
ForEach N In ActiveWorkbook.Names ' I only want to activate current
sheet but not sure how to write that yet
N.Name = WorksheetFunction.Substitute(N.Name,"old","new")' this
replaces range old tonew
Next N
EndSub</code>However, when I run the Macro - the new named range becomes 'SheetB'!new. My step 2 needs to delete this named range but can't! Please help me fix that.
Here's my step 2:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">enter code here
PublicSub RescopeNamedRangesToWorkbook()
Dim wb As Workbook
Dim ws As Worksheet
Dim objName As Name
Dim sWsName AsString
Dim sWbName AsString
Dim sRefersTo AsString
Dim sObjName AsString
Set wb = ActiveWorkbook
Set ws = ActiveSheet
sWsName = ws.Name
sWbName = wb.Name
'Loop through names in worksheet.
ForEach objName In ws.Names
'Check name is visble.
If objName.Visible =TrueThen
'Check name refers to a range on the active sheet.
If InStr(1, objName.RefersTo, sWsName, vbTextCompare)Then
sRefersTo = objName.RefersTo
sObjName = objName.Name
'Check name is scoped to the worksheet.
If objName.Parent.Name <> sWbName Then
'Delete the current name scoped to worksheet replacing with workbook scoped name.
sObjName = Mid(sObjName, InStr(1, sObjName,"!")+1, Len(sObjName))
objName.Delete
wb.Names.Add Name:=sObjName, RefersTo:=sRefersTo
EndIf
EndIf
EndIf
Next objName
EndSub
</code>The above VBA runs into error at objName.Delete because it can't delete special characters.