Copy named range contents and range definition from one workbook to another

dhregan

New Member
Joined
Mar 30, 2010
Messages
37
I have two workbooks. Workbook_A is a "shell" with the following named ranges and definitions:

Name: Parameter_1, Refers To: Sheet1!$A$11
Name: Parameter_2, Refers To: Sheet1!$A$12
Name: Parameter_3, Refers To: Sheet1!$A$13

Workbook_B - which was originally based on the shell - has the following named ranges and definitions:

Name: Parameter_1, Refers To: Sheet1!$A$11
Name: Parameter_2, Refers To: Sheet1!$A$12
Name: Parameter_3, Refers To: Sheet1!$A$13
Name: Parameter_4, Refers To: Sheet1!$A$14
Name: Parameter_5, Refers To: Sheet1!$A$15

I am trying to write a macro that will copy the contents and formats of the named ranges - as well as the named range definitions - from Workbook_B to Workbook_A. That is, when the macro is done running, Workbook_A will have the following named ranges and definitions:

Name: Parameter_1, Refers To: Sheet1!$A$11
Name: Parameter_2, Refers To: Sheet1!$A$12
Name: Parameter_3, Refers To: Sheet1!$A$13
Name: Parameter_4, Refers To: Sheet1!$A$14
Name: Parameter_5, Refers To: Sheet1!$A$15

I know how to copy the contents and formats, but I can't seem to get the named range definitions over. How can this be done? Is it possible to copy contents, formats and named range definition in one statement?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this method to copy the names and RefersTo

with the 2 workbooks set to wbA and wbB

Code:
  Dim n As Integer, nm As String
  For n = 1 To 5
      nm = "Parameter_" & n
      On Error Resume Next
      wbA.Names(nm).Delete
      wbA.Names.Add Name:=nm, RefersTo:=wbB.Names("Parameter_" & n).RefersTo
  Next
 
Last edited:
Upvote 0
Opps :oops: forgot to incude the line to copy the contents and the formats

insert this in the line below the one creating the names
Code:
wbB.Names(nm).RefersToRange.Cells.Copy wbA.Names(nm).RefersToRange.Cells
 
Upvote 0
Thanks Yongle. Because I won't necessarily know the fixed number of named ranges in Workbook_B, is there a way to copy without relying on knowing how many occurrences of the "Parameter_#" structure are present in Workbook_B? In other words, can the macro be constructed to make the copy based on a dynamic number of occurrences in Workbook_B?

If it helps, I also have a "comprehensive" named range to capture all of the named items in Workbook_B. That is, I have another named range in Workbook_B called "Range Inventory" which spans cells $A$11:$A$15. So, Range Inventory will always contain the cells with the named ranges that I want to copy from Workbook_B to Workbook_A.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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