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
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,557
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
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,557
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
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,557
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,099,122
Messages
5,466,824
Members
406,500
Latest member
Tknotmaxx

This Week's Hot Topics

Top