copy formulas from sheet to sheet and tie back to previous

craigg3

Board Regular
Joined
Dec 23, 2002
Messages
161
Office Version
  1. 2013
Platform
  1. Windows
I have a formula in sheet2 (cell A,1) that ties back to sheet1, how can I copy sheet2 and paste it into sheet3 (cell A,1) and make the formulas now tie back to sheet2 instead of sheet1. All sheets will have the same format, I just want each sheet to tie back to the previous sheet instead of all sheets tieing back to sheet 1.

I know how to manually get this done, but I have a lot of formulas that need to tie back to the previous sheet and was hoping there was a faster way of doing this instead of manually changing each sheet one by one.

Thanks!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Re: copy formulas from sheet to sheet and tie back to previo

You could use code like:

Sub ChangeLinkedFormulas()

Dim OldSheetName As String, NewSheetName As String

If ActiveSheet.Index = 1 Then Exit Sub
OldSheetName = Sheets(1).Name
NewSheetName = Sheets(ActiveSheet.Index - 1).Name

Cells.Replace OldSheetName, NewSheetName & NewSheetNo, xlPart, xlByRows, True

End Sub

By running this code on each sheet it will replace all instances of "Sheet1" (or whatever the first sheet is named) with the name of the previous sheet. You'll have to be careful though - it will replace all instances of "Sheet1" even though you may not want it to. Also, it uses the index of the sheet to to determine what to change the formula to. I believe this index is order that the sheets were added to the workbok, NOT the order you currently have them in.
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Have tried to get the code to work, it doesnt give me any errors, but it doesnt copy the previous sheet over the current sheet.
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Sorry, I didn't realise you wanted to copy it too. Try this instead. I was wrong about the index property, it actually does refer to the order that the sheets are in on the tab bar. So, running this code should copy the second sheet on the taskbar (I'm assuming this is the first of these multiple sheets with the offending formulas, ie =Sheet1..etc) to the active sheet. It then changes all of the instances of the name of sheet1 to the name of the sheet to the left of the activesheet.
It would be best to get your sheets in the order you want before running.

Sub ChangeLinkedFormulas2()

Dim OldSheetName As String, NewSheetName As String

If ActiveSheet.Index = 1 Or ActiveSheet.Index = 2 Then _
MsgBox "Cannot perform on sheet " & ActiveSheet.Name: Exit Sub
OldSheetName = Sheets(1).Name
NewSheetName = Sheets(ActiveSheet.Index - 1).Name

Sheets(2).Cells.Copy ActiveSheet.Cells

Cells.Replace OldSheetName, NewSheetName & NewSheetNo, xlPart, xlByRows, True

End Sub
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Thanks for the help, works great now!!
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Just realised that the "& NewSheetNo" in the last code line shouldn't be there. It doesn't matter, it makes no difference to the code, but it may be confusing. It was left over from when I tried a different approach.
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Just have one more question. Is there a way to make it copy the previous sheet instead of just the 2nd sheet in the tab order? I might end up adding a few things to new sheets, but dont wont it to always revert back to the 2nd sheet format, only the previous sheet. Thanks.
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

This should do it

Sub ChangeLinkedFormulas3()

Dim OldSheetName As String, NewSheetName As String

If ActiveSheet.Index = 1 Or ActiveSheet.Index = 2 Then _
MsgBox "Cannot perform on sheet " & ActiveSheet.Name: Exit Sub
OldSheetName = Sheets(ActiveSheet.Index - 2).Name
NewSheetName = Sheets(ActiveSheet.Index - 1).Name

Sheets(ActiveSheet.Index - 1).Cells.Copy ActiveSheet.Cells

Cells.Replace OldSheetName, NewSheetName, xlPart, xlByRows, True

End Sub
 
Upvote 0
Re: copy formulas from sheet to sheet and tie back to previo

Works great Craig, thanks for the help
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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