我有一张包含一堆数据的表格。然后,我有一个不同的工作表,它引用了第一张工作表中的多个单元格。如果我想删除第一张工作表并将其替换为相同的工作表(在任何意义上都相同,即工作表名称、每个单元格中的数据类型、格式等,每个单元格中的实际文本数据除外),参考另一张纸丢失了,我所有的单元格都产生了#REF!错误。
有什么方法可以保留引用并替换或覆盖工作表,而无需手动剪切和粘贴信息?
预先感谢,
乔治
我有一张包含一堆数据的表格。然后,我有一个不同的工作表,它引用了第一张工作表中的多个单元格。如果我想删除第一张工作表并将其替换为相同的工作表(在任何意义上都相同,即工作表名称、每个单元格中的数据类型、格式等,每个单元格中的实际文本数据除外),参考另一张纸丢失了,我所有的单元格都产生了#REF!错误。
有什么方法可以保留引用并替换或覆盖工作表,而无需手动剪切和粘贴信息?
预先感谢,
乔治
这是我喜欢使用的解决方案:
move
(而不是复制)要删除的工作表到新/空工作簿。Data
选项卡(Excel 菜单)Connections
►Edit Links
中。请注意,在此解决方案中,替换表在插入时必须具有相同的名称。但是,您可以(当然)在上述过程完成后更改工作表名称。
我当然希望我能充分解释它。但是,如果您需要其他解释,请随时告诉我。
创建新工作表,然后使用公式在工作表上进行查找和替换以找到原始工作表名称并替换为新工作表名称。然后删除旧工作表,并将新工作表重命名为您想要的任何名称。
重申问题:
[Sheet1] contains the raw data
[Sheet2] has references to the raw data on [Sheet1] in the form of '[Sheet1]'![Cell Reference]
You wish to update all the raw data by replacing the old [Sheet1] with a new [Sheet1].
When you delete the old [Sheet1], all the formula references on [Sheet2] change to #REF.
我的解决方法
I use Indirect Referencing to [Sheet1].
Let's assume my [Sheet1] is called "RawData".
My formulas in [Sheet2] will indirectly reference [Sheet1] as follows:
Indirect("RawData!A1")
The **big downside** to this is that if cell A1 on the [RawData] sheet is moved around, the A1 will not move. **This is NOT good.**
I almost always use Tables so I am referencing table names, not specific cells. And this method works great for that.
阅读所有评论后,我的建议是以下方法(警告,这是一种解决方法,而不是一个干净的解决方案):
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
基本上,我不是替换单元格中的工作表名称(正如@TheGuyThatDoesn'tKnowMuch 已经提出的那样),而是替换等号,暂时将单元格转换为文本,最后我再次转换等号,创建公式在所有单元格公式再次,假装。