Copying sheets but keep sheet references in formulas intact

indyman

New Member
Joined
Apr 14, 2021
Messages
47
Office Version
  1. 2019
Platform
  1. Windows
In my workbook, I have formulas on Sheet 2 that refences values on Sheet1. When I want to issue updates, my plan is to have an update button that among other things is to copy data from the wbUserCopy to the UpdatedWB. That process looks like this:

VBA Code:
            If Not sheetExist(sSheet, wbNewVersion) > 0 Then 'sheetExist is a function
                wbUserCopy.Sheets(sSheet).Copy Before:=UpdatedWB.Sheets("Sheets1")
                If sheetExist(sSheet, UpdatedWB) > 0 Then
                   'log success/failures
                End If

This all works as I want it to except for the formulas mentioned above. The formula carries the originating workbook name with it, thus creating an external link as shown here:
='[UserCopy.xlsm]User Input'!$B$3
I want to keep that as:
='User Input'!$B$3

I can certainly find "[UserCopy.xlsm]" and replace with nothing. But is there a better way, like a different way to copy the sheet?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I think you just need to use Edit Link function to change reference.

Let say I have value in Sheet1 and in Sheet2 I have a formula that link to Sheet1.
I copy Sheet1 to UserUpdateWB. Then Copy Sheet2 to UserUpdateWB. The formula in Sheet2 will have the reference to wbUserCopy. By changing reference the to UserUpdateWB, the external link will go away.

Example of my Test code
VBA Code:
Sub RelativeCopy()

Dim wbUserCopy As Workbook
Dim UserUpdateWB As Workbook

Set wbUserCopy = ActiveWorkbook
Set UserUpdateWB = Workbooks("UserUpdate.xlsx")

wbUserCopy.Sheets("Sheet1").Cells.Copy UserUpdateWB.Sheets("Sheet1").Range("A1")
wbUserCopy.Sheets("Sheet2").Cells.Copy UserUpdateWB.Sheets("Sheet2").Range("A1")
UserUpdateWB.ChangeLink Name:="UserCopy.xlsm", NewName:="UserUpdate.xlsx", Type:=xlExcelLinks

End Sub
 
Upvote 0
Solution
I think you just need to use Edit Link function to change reference.

Let say I have value in Sheet1 and in Sheet2 I have a formula that link to Sheet1.
I copy Sheet1 to UserUpdateWB. Then Copy Sheet2 to UserUpdateWB. The formula in Sheet2 will have the reference to wbUserCopy. By changing reference the to UserUpdateWB, the external link will go away.

Example of my Test code
VBA Code:
Sub RelativeCopy()

Dim wbUserCopy As Workbook
Dim UserUpdateWB As Workbook

Set wbUserCopy = ActiveWorkbook
Set UserUpdateWB = Workbooks("UserUpdate.xlsx")

wbUserCopy.Sheets("Sheet1").Cells.Copy UserUpdateWB.Sheets("Sheet1").Range("A1")
wbUserCopy.Sheets("Sheet2").Cells.Copy UserUpdateWB.Sheets("Sheet2").Range("A1")
UserUpdateWB.ChangeLink Name:="UserCopy.xlsm", NewName:="UserUpdate.xlsx", Type:=xlExcelLinks

End Sub
This works perfectly. All I had to do was add that last line of code. I knew that if there was a better way to do it, then someone on here would know. And for any other readers, if the sheets are protected even with
VBA Code:
UserInterfaceOnly= True
, then you have to unprotect them to update the links. Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
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