简体   繁体   English

将工作表复制到多个工作簿 - 公式引用

[英]Copy worksheet to multiple workbooks - formula references

I have used the following Macro, that I found on another forum to copy one sheet into multiple other worksheets: 我使用了以下宏,我在另一个论坛上发现将一张表复制到多个其他工作表中:

    Option Explicit
Public Sub CopySheetToAllWorkbooksInFolder()

    Dim sourceSheet As Worksheet
    Dim folder As String, filename As String
    Dim destinationWorkbook As Workbook

    'Worksheet in active workbook to be copied as a new sheet to the destination woorkbook

    Set sourceSheet = ActiveWorkbook.Worksheets("Sheet1")

    'Folder containing the destination workbooks

    folder = "F:\temp\excel\"

    filename = Dir(folder & "*.xls", vbNormal)
    While Len(filename) <> 0
        Debug.Print folder & filename
        Set destinationWorkbook = Workbooks.Open(folder & filename)
        sourceSheet.Copy before:=destinationWorkbook.Sheets(1)
        destinationWorkbook.Close True
        filename = Dir()  ' Get next matching file
    Wend
 End Sub

The source worksheet, that I want to copy into the other destination worksheets has formulas (related to other worksheets in the source file). 我要复制到其他目标工作表的源工作表具有公式(与源文件中的其他工作表相关)。 After running the macro, the formulas in the destination worksheet, still have references to the source worksheet instead of the destination worksheet. 运行宏后,目标工作表中的公式仍然引用源工作表而不是目标工作表。

How could I modify the macro in order to to adjust the references to the new workbook? 我如何修改宏以调整对新工作簿的引用?

Thanks a lot in advance! 非常感谢提前!

Best, Arthur 最好,亚瑟

You could just deleted the inserted references after you have copied the sheet. 您可以在复制工作表后删除插入的引用。 For example, if your source workbook is named "Source.xlsx": 例如,如果源工作簿名为“Source.xlsx”:

Cells.Replace What:="=[Source.xlsx]", Replacement:="=", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
ReplaceFormat:=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM