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

时间:2017-03-22 12:15:42

标签: excel vba copy formulas

我使用了以下宏,我在另一个论坛上发现将一张表复制到多个其他工作表中:

    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

我要复制到其他目标工作表的源工作表具有公式(与源文件中的其他工作表相关)。 运行宏后,目标工作表中的公式仍然引用了源工作表而不是目标工作表。

如何修改宏以调整对新工作簿的引用?

提前多多感谢!

最佳, 亚瑟

1 个答案:

答案 0 :(得分:0)

您可以在复制工作表后删除插入的参考。例如,如果您的源工作簿名为&#34; Source.xlsx&#34;:

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