How do I keep Cell Referencing in Excel if I replace sheet?

亡梦爱人 提交于 2019-12-11 06:07:22

问题


I have a sheet with a bunch of data. I then have a different sheet that references multiple cells in that first sheet. If I want to delete the first sheet and replace it with an identical sheet (identical in every sense, ie sheet name, data type in each cell, format, etc, except for the actual text data in each cell), the references in the other sheet are lost, and all my cells produce a #REF! error.

Is there any way of preserving the references and replacing or overwriting the sheet, without having to manually cut and paste the information?

Thank in advance,

George


回答1:


Here is a solution I like to work with:

  1. Before deleting the old sheet right-click on the sheet name and move (not copy) the sheet you wish to delete to a new / empty workbook.
  2. Now, all links within the original file are automatically converted to reference the newly created workbook and all these links show up in the Data tab (Excel menu) ConnectionsEdit Links.
  3. Now you can insert the new sheet you wish to reference and in the above menu you can change the link to reference the original file. So, you are essentially changing the reference to itself back again (thus removing the reference to the newly created external workbook).

Note, that in this solution the replacement sheet will have to have the same name when inserted. Yet, you can (of course) change the sheet name after the above process is completed.

I surely hope I explained it sufficiently. Yet, don't hesitate to let me know if you require additional explanations.




回答2:


Create the new sheet, then do a find & replace on the sheet with formulas to find the original sheet name and replace with the new sheet name. Then delete the old sheet, and rename the new sheet to whatever you want.




回答3:


After reading all the comments, my advice would be the following approach (warning, it is a work around, not a clean solution):

Sub DeleteSheets_KeepingReferences()
     Dim sht As Worksheet
     Set sht = ThisWorkbook.Sheets("")

     sht.Cells.Replace What:="=", Replacement:="#$%", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

     'Delete sheet
     Application.DisplayAlerts = False
     wb.Sheets(sht).Delete

     'Code to copy sheet

     sht.Cells.Replace What:="#$%", Replacement:="=", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub

Basically, instead of replacing the sheet name in the cells (as proposed already by @TheGuyThatDoesn'tKnowMuch), I'm replacing the equal sign, temporarily converting the cells into text, and in the end I convert the equal sign again, creating formulas in all cells formulas again, as pretended.



来源:https://stackoverflow.com/questions/37791038/how-do-i-keep-cell-referencing-in-excel-if-i-replace-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!