Multiple Named Ranges Code

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,540
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have many, many named ranges to re-name, and have been fortunate to find a code that renames them all, only i cannot get it to work properly. I wonder if someone might take a look over the code to see if anything is wrong?

Basically you type the old name in one column and the new name in the next column. You then highlight old names and run macro?

It says subscript out of range?

Thanks

Code:
Sub BatchRename()
'Uses functions from JKP's Name Manager addin:
'www.jkp-ads.com/officemarketplacenm-en.asp

'Takes a list of names (selected cells on worksheet in the file).
'Renames those names with the name which is in a column to the immediate right
'of the selected range

    Dim sOldname As String
    Dim sNewName As String
    Dim oCell As Range
    Dim lRowCount As Long
    lRowCount = Selection.rows.Count
    Application.Run "'" & Workbooks("Name Manager.xla").FullName & "'!InitNameManager"
    For Each oCell In Selection.Cells
'ActiveSheet.UsedRange.Columns(1).SpecialCells(xlCellTypeVisible)
        If oCell.row > 1 Then
            sOldname = oCell.Value
            sNewName = oCell.Offset(, 1).Value
            Application.StatusBar = sOldname & ", " & sNewName & ", " & Format(oCell.row / lRowCount, "0%")
            If sOldname <> sNewName And sOldname <> "" And sNewName <> "" Then
                Application.Run "'" & Workbooks("Name Manager.xla").FullName & "'!replacename", sOldname, sNewName, True
            End If
        End If
    Next
    Application.StatusBar = False
    Application.Visible = True
End Sub
 

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
Apologies if this is a dumb question, but have you installed 'name manager.xla'?
 
Upvote 0
Hi Weaver,

Yes it is installed and works fine.

Any ideas why the code is not working correctly?

Many Thanks
 
Last edited:
Upvote 0
Looking at it, what row does your selected range start on, and does it contain a header?

At what point does the code fail?
 
Upvote 0
These are the only instruction given

I can only assume you have to just list the 2 columns side by side and highlight the existing named ranges and the code will do the rest for you

Code:
9	Renaming many range names
Renaming a range name is possible with Name Manager, but if you have a lot of names to change, perhaps this method is simpler:

-	Create a two-column list, old names in first column, new names in second.
-	Select the cells in the first column and run this routine (assuming you have Name Manager installed!):
 
Upvote 0
Hi Guys,

Maybe some kind person can re-write the code necessary

What i would like to do, is list out in one column (any column) the old named ranges and the next column (immediate right) the new named ranges

I would like to just highlight the ones i would like re-named and then run the code

example

Bob_Hours_1 Bob_Hours_001
Neil_Hours_1 Neil_Hours_001
Alan_Hours_10 Alan_Hours_010
Lee_Hours_12 Lee_Hours_012

Hope this makes sense

Thanks in advance
 
Upvote 0
The original code is throwing up a Runtime 9 error

and is failing at the Application.Run line

Any thoughts?
 
Upvote 0
Sorted:

Hi,

Rename the

Application.Run "'" & Workbooks("Name Manager.xla").FullName & "'!InitNameManager"

to:

Application.Run "'" & Workbooks("Name Manager 2007.xlam").FullName & "'!InitNameManager"

and the

Application.Run "'" & Workbooks("Name Manager.xla").FullName & "'!replacename", sOldname, sNewName, True

to:

Application.Run "'" & Workbooks("Name Manager 2007.xlam").FullName & "'!replacename", sOldname, sNewName, True

Works perfecto!
 
Upvote 0
Lol, just a case of dodgy filenames then.

Just for the record, here's a dodgy bit of code I wrote, with zero error checking

Code:
Sub test()
    For Each c In Selection
        oldname = c.Value
        newname = c.Offset(, 1).Value
        Range(oldname).Name = newname
        ActiveWorkbook.Names(oldname).Delete
    Next
End Sub
So long as you know what you're doing and there are no name conflicts, it appears to work, but since you got the nice version up and running, it's all good.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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