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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,980
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:

Nine Zero

Well-known Member
Joined
Mar 10, 2016
Messages
622
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
 

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88
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.
 

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88

ADVERTISEMENT

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
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,980
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?
 

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88

ADVERTISEMENT

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:

Damo71

Board Regular
Joined
Aug 17, 2010
Messages
88
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:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,980
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,942
Members
416,889
Latest member
dhegs

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
Top