Can I change the reference to a named range via VBA?

  • Thread starter Thread starter Bassman62
  • Start date Start date
B

Bassman62

I have a range named "Home" that refers to: =Sheet1!$A$1.
What Macro code would I use to change the reference?
Thanks for any advice.
 
Clarification:
The named range "Home" is a defined name in the worksheet, not a variable
in any code.
 
Here is how I do it. Say you want to move the named range from A2 to B3.

Sub dk()
ActiveWorkbook.Names("Home").Delete
ActiveWorkbook.Names.Add "test", RefersTo:="=Sheet1!$B$3"
End Sub

It will also work for a range like Sheet1!$A$2:$C$12
 
Sorry about the typo, should have been Home in both places.

Sub dk()
ActiveWorkbook.Names("Home").Delete
ActiveWorkbook.Names.Add "Home", RefersTo:="=Sheet1!$B$3"
End Sub
 
You can just reassign the Name to a different range:

worksheets("sheet99").range("z9").name = "Home"
 
ActiveWorkbook.Names("Home").Delete

You don't need to delete the name, although it is harmless to do so
and it adds clarity to the logic of the code.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
Thanks to Chip and Dave.
This helps my understanding of referencing named ranges in VBA.
 
Back
Top