Create copy workbook with cell referencing to original

Faith

New Member
Joined
Apr 15, 2014
Messages
13
Hello,

At my work we are using Excel as a CMS to produce SQL scripts for insertion into a database. The data is then turned into mobile web pages. This involves multiple workbooks - 1 for each main page with the sub-pages as extra worksheets in the book.

Much of the data is duplicated so we have created master workbooks then copied them and laboriously referenced the relevant cells back to the original. That way if any data in the master is changed, all copies will change simultaneously.

This is obviously not the most efficient way of publishing web pages but we are stuck with it for now. So I am wondering if it is possible to write a VBA code to create a copy of the master and then reference all required tables in the worksheets automatically? The tables are all named ranges.

Any help is really appreciated.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It sounds as if VBA could be used to do that, could you please provide a specific example of what is needed. How many worksheets/cells would have to be updated in the copy?
 
Upvote 0
Hi Phil

Thanks for your reply. The workbooks have from 10 - 30 worksheets in them with multiple named ranges on each worksheet. The majority of the information in the named ranges will be duplicated across all copies but some of the cells will still need to be editable on a couple of the worksheets.

All of these ranges will be referenced to the master workbook and identical on each copy workbook. There are 14 copy workbooks for that one master.

Each worksheet has different named ranges, some of which provide data validation for each other. The names cannot change as they are part of the current VBA code that produces the SQL code.

So we want a code that works:

Step1: Make a copy of the Master
Step 2: Worksheet 1, Named Range 1 on Copy workbook references to Master Worksheet1, Named Range 1
Step 3: Worksheet 1, Named Range 2 on Copy workbook references to Master Worksheet1, Named Range 2
and so on

I can email you an example if that would be helpful.

Thanks
Faith
 
Upvote 0
Faith,

Create a new workbook named Copier.xlsm, add a standard module and put this code in it.
Modify the code to include the correct Master file Name and Extension and the Copy file names
Save Copier.xlsm in the same directory as the Master file. Copies will be created in that directory
With the master file closed, Press Alt+F8 to get a list fo the macros. Highlight the one named'
CreateCopiesWithNamedRangesReferencingMaster and press run.

Please let me know how it works or if changes are needed.

Code:
Option Explicit

Sub CreateCopiesWithNamedRangesReferencingMaster()
    'Create a new workbook named Copier.xlsm, add a standard module and put this code in it.
    'Save Copier.xlsm in the same directory as the Master file.  Copies will be created in that directory

    Dim aryCopyNames As Variant
    Dim lX As Long, lY As Long
    Dim sCopyName As String
    Dim secAutomation As MsoAutomationSecurity
    Dim sExtension As String
    Dim sMasterFileName As String
    
    'Modify the next line to contain the name of the Master workbook, be sure to include the extension
    sMasterFileName = "Master.xlsx"
    'An example for the above line would be sMasterFileName = "Master.xlsx"
    
    'Modify next line to contain names of each copy. extension will be copied from the extension in sMasterFileName
    aryCopyNames = Array("CopyAA", "CopyBB", "CopyCC")
    'An example for the above line would be aryCopyNames = Array("CopyA", "CopyB", "CopyC")

    secAutomation = Application.AutomationSecurity                          'Save ThisWorkbook security setting
    Application.AutomationSecurity = msoAutomationSecurityForceDisable      'Disable macros when opening file
    
    sExtension = Mid(sMasterFileName, InStrRev(sMasterFileName, "."))
    
    'Open the master file
    Workbooks.Open Filename:=ThisWorkbook.Path & "\" & sMasterFileName
    
    For lX = LBound(aryCopyNames) To UBound(aryCopyNames)
    
        If UCase(Right(aryCopyNames(lX), Len(sExtension))) <> UCase(sExtension) Then
            sCopyName = aryCopyNames(lX) & sExtension
        Else
            sCopyName = aryCopyNames(lX)
        End If

        'Save a copy of the master file
         Workbooks(sMasterFileName).SaveCopyAs Filename:=ThisWorkbook.Path & "\" & sCopyName
        'Open the copy
        Workbooks.Open Filename:=ThisWorkbook.Path & "\" & sCopyName
        
        'Update named ranges in the copy
        For lY = 1 To ThisWorkbook.Names.Count
            With Workbooks(sCopyName).Names(lY)
                .RefersTo = "=[Master.xlsm]" & Mid(.RefersTo, 2)
            End With
        Next
        
        'Save and close the copy
        Workbooks(sCopyName).Save
        Workbooks(sCopyName).Close
        
    Next
    
    'Close the master file
    Workbooks(sMasterFileName).Close
    
End_Sub:

    Application.AutomationSecurity = secAutomation                          'Restore ThisWorkbook security setting
    
End Sub

Sub ListNamedRanges()

    Dim lX As Long
    
    Debug.Print "====================================="
    Debug.Print "Named Ranges in: " & ActiveWorkbook.Name
    Debug.Print "====================================="
    For lX = 1 To ActiveWorkbook.Names.Count
        With ActiveWorkbook.Names(lX)
            Debug.Print "Name:           " & .Name
            Debug.Print "RefersToR1C1:   " & .RefersToR1C1
            Debug.Print "RefersTo:       " & .RefersTo
            Debug.Print "Comment:        " & .Comment
            Debug.Print "-------------------------------------"
        End With
    Next
End Sub
 
Upvote 0
Hi Phil

Thank you so much for your help. I followed your instructions and the copying of the files worked perfectly. However I don't know if I'm missing something but the referencing did not work.

Do I need to be entering the range names somewhere? The ListNamedRanges macro doesn't seem to be called anywhere in the CreateCopiesWithNamedRangesReferencingMaster macro?
 
Upvote 0
Faith,

ListNamedRanges shows you details of ranges in the active workbook.

If CreateCopiesWithNamedRangesReferencingMaster made the copies, it should have modified the ranges in each copy to reference the master.

Open one of the copies and run the ListNamedRanges to see the details of the named ranges in it. Do they reference the master file? or the copy?

If the ranges were not modified, please post the copy of CreateCopiesWithNamedRangesReferencingMaster that you edited (to have the name of your master file and copies) as well as the output from ListNamedRanges for the first range (or last range) for the master workbook and one of the copies.
 
Upvote 0
Here is the part I modified:

'Modify the next line to contain the name of the Master workbook, be sure to include the extension
sMasterFileName = "DOG_Deal1_Coke_Master.xlsm"
'An example for the above line would be sMasterFileName = "Master.xlsx"

'Modify next line to contain names of each copy. extension will be copied from the extension in sMasterFileName
aryCopyNames = Array("DOG_Deal1_Coke_C", "DOG_Deal1_Coke_F", "DOG_Deal1_Coke_S")
'An example for the above line would be aryCopyNames = Array("CopyA", "CopyB", "CopyC")

That part seems to work fine. The rest I just copied and pasted from your post.

Running the ListNamedRanges macro does not seem to do anything. I tested it by making a named range in my Copier workbook but nothing happened when I ran the macro. It also didn't work when I ran it from one of the copies or when I copied the macro itself into the copy workbook module.

The ranges in the new copy workbooks are definitely not referencing the master or themselves, the text in the cells just appears as normal.
 
Upvote 0
The ListNamedRanges sub displays its results in the immediate pane of the VBEditor.

The following version will document the active workbook's named ranges on a worksheet in that workbook:

Code:
Sub Document_ListActiveWorkbookNamedRangesOnAWorksheet()

    Dim lX As Long
    
    'Initialize Documentation Worksheet
    On Error Resume Next
    Application.DisplayAlerts = False
    Worksheets("Document_NamedRanges").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    Worksheets.Add(Before:=Sheets(1)).Name = "Document_NamedRanges"
    
    With Worksheets("Document_NamedRanges")
        .Range("A1").Resize(1, 5).Value = Array("Name", "RefersToR1C1", "RefersTo", "Value", "Comment")
        
        'Examine each named range
        For lX = 1 To ActiveWorkbook.Names.Count
            With ActiveWorkbook.Names(lX)
                Worksheets("Document_NamedRanges").Cells(lX + 1, 1).Value = .Name
                Worksheets("Document_NamedRanges").Cells(lX + 1, 2).Value = "'" & .RefersToR1C1
                Worksheets("Document_NamedRanges").Cells(lX + 1, 3).Value = "'" & .RefersTo
                Worksheets("Document_NamedRanges").Cells(lX + 1, 4).Value = .Value
                Worksheets("Document_NamedRanges").Cells(lX + 1, 5).Value = .Comment
                Debug.Print .Name, .RefersTo
            End With
        Next
        .UsedRange.Columns.AutoFit
    End With
        
End Sub

Do you use the OFFSET formula in any of your named ranges?
 
Upvote 0
Still can't get the ListNamedRanges macro to do anything. I ran it from within the editor but it looks like nothing is happening.

The new code you added to create an additional worksheet listing all the named ranges works. There are 153 named ranges in the copy workbook.

No there are no OFFSET formulas used anywhere in the workbook.
 
Upvote 0
To open the immediate window in the VBEditor, while in the editor press Ctrl+G. You should be able to see the output of the ListNamedRanges then.

Any Debug.Print (variable) lines in code writes the (variable) values to the Immediate window (even if it is not visible).

Please post the first 10 lines on the Document_NamedRanges worksheet for the Master and one of the Copies
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

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