Change name of named range across VBA code

danco172

New Member
Joined
Apr 1, 2010
Messages
3
I've inherited some VBA code that makes use of named ranges in a shorthand way where "xTodaysPrice" is a named range of one cell:

Code:
If iCol = [xTodaysPrice].Column Then...
The name needs to be changed now to something else, e.g. "xAvgPrice".

Besides using search/replace across the whole code is there a better way of setting this up so that in the future there is only one location that needs to change the name of the named range and the code automatically picks this up?

Thanks,
Dan
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Not sure if this is exactly what you need:

Code:
Sub Lime()
    x = InputBox(Prompt:="Type the named range to be changed.")
    y = InputBox(Prompt:="Type the new name for this range.")
    Range(x).Name = y
    ActiveWorkbook.Names(x).Delete
End Sub

I hope this helps :biggrin:
 
Upvote 0
Sorry - I have just re-read what you are asking. You want to be able to change the range in the code.

In that case: If you set x = "AvSales" or whatever near the start of the code, anytime after that you refer to call the named range x. If you need to change the Range Name this should just mean a single range at the top of your code.

I hope this helps more than my last answer. At least someone may have some use for it... :biggrin:
 
Upvote 0
Hi Dan
Welcome to the board

Change the syntax and access the named range with a range object. This way you use a string that you can initialise with the name of the named range in just one place, like:

Code:
Dim sRange As String
 
'...
 
sRange = "xTodaysPrice"
 
'...
 
If iCol = Range(sRange).Column Then
 
Upvote 0
Thank you, both. That works.

My user also likes to use the Name Manager UI to manage the named ranges. Is there a way to catch his changes and pass them on to the code?

E.g., if he just changes the name of a range, can I catch it and change my String variable accordingly?

Thanks,
Dan
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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