Copying contents of named range from one workbook to another with identical structure

OxfordCurmudgeon

New Member
Joined
Oct 24, 2023
Messages
14
Office Version
  1. 2010
  2. 2003 or older
Platform
  1. Windows
I have a complex workbook (13 worksheets, scads of formulas, lots of data, modest amount of VBA code) developed for a client. Buried in the mess is a named range of data which the client updates regularly. When I send the client a new version of the workbook, I want it to be easy for them to copy the data in that named range from the old workbook to the new. It ought to be a piece of cake: Open the old workbook, copy the data from the named range to an array, close the old workbook and move the data from the array into the named range of the new workbook.

One wrinkle: The named range has workbook scope -- and might not be on the same sheet in the old workbook as on the new one.

The problem: Every technique I have tried to copy from the named range in the old workbook ends up taking the data instead from the same named range in the current workbook.

Here's the current code:

VBA Code:
SrceWorkbookFilespec = _
    Application.GetOpenFilename("Excel Files (*.xlsm),*.xlsm", , "Open workbook with data to copy")
If SrceWorkbookFilespec = False Then Exit Sub ' Give up
Set SrceWorkbook = Workbooks.Open(SrceWorkbookFilespec, , True)   ' Open as read-only
With SrceWorkbook
'    Debug.Print .Name
    DataHold = Range(SrceWorkbook.Names("EmployeeInfo").RefersTo).Value2
    .Close SaveChanges:=False
End With

When the Debug.Print command isn't commented out, it shows -- as expected -- the name of the old workbook. But after the next line executes, DataHold has the data from the EmployeeInfo named range in the new workbook, not the old.

The VBA code is running from the new workbook. In my current testing, the new and old workbooks have nearly identical structure, so the named range refers to the same cell range ($A$2:$B$200) on the same sheet name.

And just to round out my frustration, when I add this

Range(ActiveWorkbok.Names("EmployeeInfo").RefersTo).Value2

to the Watch window, it shows me the data from the old workbook. But if I change "SrceWorkbook" to "ActiveWorkbook" in the DataHold = statement, DataHold still ends up with the data from the new workook not the old.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
What happens if you put a period "." in front of the Range so that is uses the With statement
Rich (BB code):
DataHold = .Range(SrceWorkbook.Names("EmployeeInfo").RefersTo).Value2
 
Upvote 0
Ignore my previous comment.
Since SrceWorkbook should be the active workbook when you are retrieving the named range, you should only need this:
VBA Code:
    DataHold = Range("EmployeeInfo").Value2
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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