Home » excel » excel – Copy sheet while updating the cell formula references

excel – Copy sheet while updating the cell formula references

Posted by: admin April 23, 2020 Leave a comment


I have 5 sheets in a workbook. When I “manually” copy 2 of them (in the same workbook), the references (in the cell formulas) are changed automatically for the new copied sheets.
For example in some cell I have the following formula:


And after I copy the new sheets, the formulas in the new sheets changes to:

=IF('1_Result (2)'!B5="";NA();'1_Result (2)'!B5*$C$3)

Where “1_Result” is the original sheet and “1_Result (2)” is the copied sheet.
When I do the copying with the following code:

t1.Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = nshn & "_Table"

…the formulas in the copied sheet don’t change the references, so it stays the same like:


How should I modify the code, so that the references are changed?

How to&Answers:

As I mentioned in comments, if you want references to be updated, copy two sheets at the same time:

Sheets(Array("Sheet1", "1_Result")).Copy After:=Sheets(Sheets.Count)

It works for me:

Sheet1 initially has formula ='1_Result'!B5 and after copying thouse sheets Sheet1 (2) has formula ='1_Result (2)'!B5


As simco noticed, I didn’t copied the sheets together at once, but one by one.
The following code solves the problem:

Sheets(Array("1_Table", "1_Result")).Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count - 1).Name = nshn & "_Table"
Sheets(Sheets.Count).Name = nshn & "_Result"


While trying to figure out what is wrong, I found an alternative solution which is definitely not recommended to use, but might be helpful to someone. I used a function that replaces a string with another in a single sheet. Here’s the code:

Function FormulaFindAndReplace(sh As Worksheet, phrase As String, replacement As String)
With sh
    Set Found_Link = Cells.Find(what:=phrase, After:=ActiveCell, _
        LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByRows, _
        searchdirection:=xlNext, MatchCase:=False)
    While UCase(TypeName(Found_Link)) <> UCase("Nothing")
       Found_Link.Formula = Replace(Found_Link.Formula, phrase, replacement)
       Set Found_Link = Cells.FindNext(After:=ActiveCell)
End With
End Function