Question

I have 2 workbooks. A Source workbook and a destination workbook. They are completely same except for 1 worksheet which has the same name in both but different data (both contain around 30 sheets). What i wanted was to Copy the rest of the identical worksheets from source workbook to destination workbook leaving that 1 worksheet which defers in data.

Basically the identical worksheets present in the destination workbook should be replaced with the ones from the source workbook. The worksheets contain formulas and named ranges. I was successfully able to write the VBA Code to copy the worksheets. But since the named ranges have a workbook scope. The named ranges still refer to locations in the source workbooks. So i get 2 named ranges with the same name. Something like:

'The one already present in the destination workbook (from the worksheet which was replaced)
Name=VaccStart , Refers To =Sheet2!$A$2 
'The one due to the copied worksheet.
Name=VaccStart , Refers To =[C:\Users\.....\Source.xls]Sheet2!$A$2 

I want the named ranges to refer to the destination workbook and not the source workbook when i copy them. Since all the sheets in both the workbooks are same and i am just replacing them.

Was it helpful?

Solution

One easy way to get around inadvertent link creation when moving from a source to destination workbook is to relink the destination workbook from Source to itself

Screenshot for xl2010

  • Edit .... Links
  • 'Change Source" and pick the current file as the new source

enter image description here

OTHER TIPS

This will modify the named ranges to remove the external file reference:

Sub ResetNamedRanges()
    Dim nm As Name
    Dim sRefersTo As String
    Dim iLeft As Integer
    Dim iRight As Integer

    For Each nm In ActiveWorkbook.Names
        sRefersTo = nm.RefersTo
        iLeft = InStr(sRefersTo, "[")
        iRight = InStr(sRefersTo, "]")
        If iLeft > 1 And iRight > 0 Then
            sRefersTo = Left$(sRefersTo, iLeft - 1) & Mid$(sRefersTo, iRight + 1)
            nm.RefersTo = sRefersTo
        End If
    Next nm
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top