Is there a way to copy named ranges to another workbook?

Burrgogi

Active Member
Joined
Nov 3, 2005
Messages
363
Office Version
  1. 2010
Platform
  1. Windows
I have 2 workbooks that are similar to each other. Each workbook contains several worksheets.

Workbook #1:
sheet 1
sheet 2
sheet 3
sheet 4
etc.

Workbook #2:
sheet 1
sheet 2
sheet 3
sheet 4

There are named ranges all over the workbook in various locations. The only one that I care about is sheet 4. I want to copy all the named ranges into Workbook #1 'sheet 4' over to Workbook #2 'sheet 4'

I know I can just as easily use the copy worksheet feature in Excel but that poses a separate problem for me. I have hyperlinks in 'sheet 1' pointing to these named ranges and when I copied 'sheet 4' from one workbook to another, I found that it really messes up the hyperlinks.

I've googled for an answer but I'm not getting anything useful.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See if the following comes close to what you want. You'll need to adjust the actual workbook & worksheet names to suit your actual case, and the code assumes you wanted the named ranges copied to the same locations in workbook 2 where they came from in workbook 1. Assumes both workbooks are open when you run the code, and that the code is housed in workbook 1.

VBA Code:
Option Explicit
Sub Burrgogi()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks("Workbook Two")
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb1.Worksheets("Sheet4")
    Set ws2 = wb2.Worksheets("Sheet4")
    
    Dim RngNm, c
    For Each RngNm In wb1.Names
        ws1.Activate
        If RngNm.RefersToRange.Parent.Name = ActiveSheet.Name Then
            c = ws1.Range(RngNm).Cells(1).Address
            ws1.Range(RngNm).Copy ws2.Range(c)
        End If
    Next RngNm
End Sub
 
Upvote 0
See if the following comes close to what you want. You'll need to adjust the actual workbook & worksheet names to suit your actual case, and the code assumes you wanted the named ranges copied to the same locations in workbook 2 where they came from in workbook 1

Sorry I should have mentioned that the cell addresses will be slightly different. Maybe it will help if I upload a brief screenshot of the 2 worksheets in question.

The named ranges are contained in column A only - nowhere else.

Each DLC has a named range. In a case with multiple DLCs such as "Bioshock Infinite" shown at the top of the screenshot, I grouped all 3 and named it as "Bioshock Infinite". I hope this makes it more clear what I'm trying to do.
 

Attachments

  • Epic DLC sheet.png
    Epic DLC sheet.png
    145.8 KB · Views: 13
Upvote 0
Sorry I should have mentioned that the cell addresses will be slightly different. Maybe it will help if I upload a brief screenshot of the 2 worksheets in question.

The named ranges are contained in column A only - nowhere else.

Each DLC has a named range. In a case with multiple DLCs such as "Bioshock Infinite" shown at the top of the screenshot, I grouped all 3 and named it as "Bioshock Infinite". I hope this makes it more clear what I'm trying to do.
So is it the case that you want each named range copied to the next available empty row in the destination sheet? And is it the case that even "DLC's" with a single row in the source sheet would also be a named range? Does the destination sheet need to be cleared of existing data first?
 
Upvote 0
So is it the case that you want each named range copied to the next available empty row in the destination sheet? And is it the case that even "DLC's" with a single row in the source sheet would also be a named range? Does the destination sheet need to be cleared of existing data first?

Good questions. I can see how this is going to be quite difficult due to the varying contents of each cell.

The more I think about this, I'm beginning to think I should make the edits by hand. To answer your questions:

1) So is it the case that you want each named range copied to the next available empty row in the destination sheet?

No. I need the contents of the cell or cells matched. In the example of the "Europa Universalis IV" the 3 DLCs are contained in rows 26-28. However in the destination worksheet, they are in rows 31-34 because there were new entries created above these rows.

2) And is it the case that even "DLC's" with a single row in the source sheet would also be a named range?
Yes

3) Does the destination sheet need to be cleared of existing data first?
Yes

Like I said, I can see how this could be quite difficult. If it's too laborious or time consuming - please don't bother writing a VBA code. This is strictly for my personal game library record keeping and nothing related for work so I would hate for you to spend a lot of time on it. Thanks.
 
Upvote 0
I think you may be in danger of overcomplicating this. Try the following code (change workbook/worksheet names as appropriate) on a copy of your files and check whether the links problem still exists.

VBA Code:
Option Explicit
Sub Burrgogi_v2()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks("Workbook Two")
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb1.Worksheets("Sheet4")
    Set ws2 = wb2.Worksheets("Sheet4")
    
    'First clear workbook 2, sheet 4
    ws2.UsedRange.Offset(1).ClearContents
    
    'Copy new data next
    ws1.UsedRange.Offset(1).Copy ws2.Cells(2, 1)
End Sub
 
Upvote 0
I think you may be in danger of overcomplicating this. Try the following code (change workbook/worksheet names as appropriate) on a copy of your files and check whether the links problem still exists.

VBA Code:
Option Explicit
Sub Burrgogi_v2()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks("Workbook Two")
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb1.Worksheets("Sheet4")
    Set ws2 = wb2.Worksheets("Sheet4")
  
    'First clear workbook 2, sheet 4
    ws2.UsedRange.Offset(1).ClearContents
  
    'Copy new data next
    ws1.UsedRange.Offset(1).Copy ws2.Cells(2, 1)
End Sub

I just tried your code and it's getting caught up by the "Sheet4" reference. The actual name of the worksheet is: DLC Sheet (Epic only)

I said it was "Sheet 4" in my original post to make things quicker and simplify things - I guess I shouldn't have done that.

Anyways, I tried modifying your code to this & it's still not working.

Set ws1 = wb1.Worksheets("'DLC Sheet (Epic only)'")
 
Upvote 0
I tried modifying your code to this & it's still not working.

I what way is is not working? Are you getting an error message when you try to run the code, and if so, what does it say?

I changed the sheet 4 name to "DLC Sheet (Epic only)" on my test file & the copy worked fine for me.

VBA Code:
Option Explicit
Sub Burrgogi_v3()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks("Workbook Two")
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb1.Worksheets("DLC Sheet (Epic only)")
    Set ws2 = wb2.Worksheets("Sheet4")
    
    'First clear workbook 2, sheet 4
    ws2.UsedRange.Offset(1).ClearContents
    
    'Copy new data next
    ws1.UsedRange.Offset(1).Copy ws2.Cells(2, 1)
End Sub

If it still doesn't work for you, I don't think there's much more I can suggest without seeing your actual file - which you could possibly share via Dropbox, Google Drive etc?
 
Upvote 0
I what way is is not working? Are you getting an error message when you try to run the code, and if so, what does it say?

I changed the sheet 4 name to "DLC Sheet (Epic only)" on my test file & the copy worked fine for me.

VBA Code:
Option Explicit
Sub Burrgogi_v3()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks("Workbook Two")
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = wb1.Worksheets("DLC Sheet (Epic only)")
    Set ws2 = wb2.Worksheets("Sheet4")
  
    'First clear workbook 2, sheet 4
    ws2.UsedRange.Offset(1).ClearContents
  
    'Copy new data next
    ws1.UsedRange.Offset(1).Copy ws2.Cells(2, 1)
End Sub

If it still doesn't work for you, I don't think there's much more I can suggest without seeing your actual file - which you could possibly share via Dropbox, Google Drive etc?

The error message says: Subscript out of range

Here's a screenshot.

Oh and 1 more thing. I want to clarify that both of the workbooks have a worksheet with the name "DLC Sheet (Epic only)". I don';t know if that is why the error is happening or not.
 

Attachments

  • subscript out of range.png
    subscript out of range.png
    81.3 KB · Views: 6
Last edited:
Upvote 0
The error message says: Subscript out of range

Here's a screenshot.

Oh and 1 more thing. I want to clarify that both of the workbooks have a worksheet with the name "DLC Sheet (Epic only)". I don';t know if that is why the error is happening or not.
Is that a space you've got between the closing parenthesis and the double quotation mark in your version of the code? If it is - there's your problem, the code is looking for a sheet name with a space at the end of its name.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,853
Members
449,194
Latest member
HellScout

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