Programatically add prefix to all named ranges

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88
In my workbook I have many named ranges. I want to add a prefix "P_" to each name.

Is there a way I can do this with VBA to save time?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You could use this sub
Code:
Sub Test()
    Dim oneName As Name
    For Each oneName In ThisWorkbook.Names
        If TypeName(Evaluate(oneName.RefersTo)) = "Range" Then
            If Not (oneName.Name Like "P_*") Then
                oneName.RefersToRange.Name = "P_" & oneName.Name
                oneName.Delete
                Call Test
                Exit Sub
            End If
        End If
    Next oneName
End Sub
 
Last edited:
Upvote 0
Hey try this out

Code:
Sub AddPre()
Dim dataCol As Range
Dim r As Range
Set dataCol = Range("A1:A15")
For Each r In dataCol
r.Value = "P_" & r.Value
Next r
End Sub

just change the range to where ever your data is
 
Upvote 0
You could use this sub
Code:
oneName.RefersToRange.Name = "P_" & oneName.Name

Thanks a lot for your help.

I'm getting the error "Wrong number of arguments or invalid property assignment" error message when I execute the code, it's showing the error on the above line.
 
Upvote 0
Hey try this out

Code:
Sub AddPre()
Dim dataCol As Range
Dim r As Range
Set dataCol = Range("A1:A15")
For Each r In dataCol
r.Value = "P_" & r.Value
Next r
End Sub

just change the range to where ever your data is

Hi, thanks for your help too.

Sorry, perhaps my explanation was bad. I mean I need to change the Named Ranges themselves, not the information in the cells to which they refer.

So let's say I have named A1:A15 "MyFirstRange", I need to change the name of the range to "P_MyFirstRange"

Thanks again
 
Upvote 0
Thanks a lot for your help.

I'm getting the error "Wrong number of arguments or invalid property assignment" error message when I execute the code, it's showing the error on the above line.
Hmm.. it works for me. What is the value of oneName.Name and OneName.RefersToRange.Address when the error occurs?
 
Upvote 0
Hmm.. it works for me. What is the value of oneName.Name and OneName.RefersToRange.Address when the error occurs?
Hi Mike

How would I go about finding that out?

P.S. Most of the names are dynamic, would that make a difference?
 
Last edited:
Upvote 0
What would the RefersTo of a named range be, typically.
Oh, I see, sorry. So one named range is "BroIsOnHoliday" and the formula in the RefersTo box is:
Code:
=OFFSET(BroList!$AK$1,1,0,COUNTA(BroList!$A:$A)-1,1)
Most have similar structures, but some are straight forward references like
Code:
(RefersTo:) =Home!$A$1:$A$2
 
Last edited:
Upvote 0
Try this
Code:
Sub Test()
    Dim oneName As Name
    For Each oneName In ThisWorkbook.Names
        If Not (oneName.Name Like "P_*") Then
            ThisWorkbook.Names.Add Name:= "P_" & oneName.Name, RefersTo:=oneName.RefersTo
            oneName.Delete
        End If
    Next oneName
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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