【问题标题】:Copying worksheets to another workbook, have references point to new workbook将工作表复制到另一个工作簿,引用指向新工作簿
【发布时间】:2014-05-09 17:27:34
【问题描述】:

这是我面临的情况:

我有一个包含两个工作表的工作簿,“调查”和“配置”。配置工作表包含链接到调查工作表的单元格。

我正在尝试将这些工作表复制到新工作簿中。但是,一旦我完成了复制,新工作簿中“配置”选项卡中的单元格就会指向旧工作簿。我想到的一种解决方案是将值复制到新工作表/新工作簿,但我的客户不希望这样:他们希望公式仍然存在,以防他们想要更新“调查”选项卡上的某些数据并让它继续配置。

有没有办法复制工作表以使任何单元格引用保持不变(即仍然指向“Survey!A1”而不是指向“OldWorkbook!Survey!A1”?

编辑 我正在尝试使用 Worksheets(Array(...)).Copy 方法。我现在遇到的问题是我有一个字符串数组,其中包含两个源工作簿共有的工作表名称。如果我用数组替换 Array(...),代码就会崩溃。不幸的是,我不提前知道源工作簿将共用哪些工作表,因此我无法硬编码 Array 函数的列表,并且我尝试创建一个看起来像 Array 的参数列表的字符串并使用它,但代码也不喜欢那样。


    'Initialize indexes and arrays and stuff
    intSheetsInCommonIndex = 1
    intDiscrepanciesIndex = 1
    intSheetsInNPEIndex = 1
    lngDiscrepancyBackColor = RGB(202, 6, 95)
    Application.ScreenUpdating = False

    For intInner = 1 To 40
        strSheetsInCommon(intInner) = ""
        strSheetsInNPE(intInner) = ""
    Next intInner

    'Set up the pointers to the two source workbooks.
    Set xlNPEWorkbook = Application.Workbooks.Open(Sheets("Combine PE Workbooks").txtNPEDocLocation)
    Set xlSPEWorkbook = Application.Workbooks.Open(Sheets("Combine PE Workbooks").txtSPEDocLocation)

    'Now, fill the strSheetsInNPE array
    For Each xlNPECurrentSheet In xlNPEWorkbook.Sheets
        strSheetsInNPE(intSheetsInNPEIndex) = xlNPECurrentSheet.Name
        intSheetsInNPEIndex = intSheetsInNPEIndex + 1
    Next xlNPECurrentSheet

    'Now go through the SPE document.  If the workhseet's name is in strSheetsInNPE, add that name to strSheetsInCommon.
    For Each xlSPECurrentSheet In xlSPEWorkbook.Sheets
        For intInner = 1 To intSheetsInNPEIndex
            If xlSPECurrentSheet.Name = strSheetsInNPE(intInner) Then
                strSheetsInCommon(intSheetsInCommonIndex) = xlSPECurrentSheet.Name
                intSheetsInCommonIndex = intSheetsInCommonIndex + 1
                Exit For
            End If
        Next intInner
    Next xlSPECurrentSheet

    'Turn alerts off (so that the messages about named ranges don't show up) and turn on the hourglass.
    Application.DisplayAlerts = False
    Application.Cursor = xlWait

    'Create the combined document
    Set xlCombinedWorkbook = Workbooks.Add
    xlCombinedWorkbook.UpdateLinks = xlUpdateLinksNever

    'Go through the NPE document and add all worksheets from there to the new workbook.

    xlNPEWorkbook.Worksheets(Array(strSheetsInCommon)).Copy xlCombinedWorkbook.Sheets(1)

如果有人对我如何使用我的工作表名称数组进行多工作表复制有任何建议,我将不胜感激。

感谢您的帮助!

【问题讨论】:

  • 看看this Stackoverflow Q/A。同时复制它们,它应该可以工作。此外,如果您在发布时提供您已经尝试过的内容,它会有所帮助,因为它有助于在您的代码上下文中提供有针对性的响应。
  • 谢谢,阿达赫。我想做的有点复杂,我不想发布一个三页长的问题。这个链接看起来很有趣,我得试试。谢谢!

标签: excel vba copy formulas


【解决方案1】:

也许我忽略了一些细节,但为什么不简单地使用 SaveAs 将旧工作簿保存到新工作簿?参考将到位,您将拥有一本新的工作簿。

【讨论】:

  • 我曾想过使用 SaveAs,但我最终想做的是将两个工作簿合并为一个,我不确定使用 SaveAs 启动新工作簿是否是最好的方法做。不过,谢谢。
  • 新工作簿是否以编程方式创建?我发现如果您使用“复制到新工作簿”功能,它不会链接到旧工作簿。代码示例:Sheets(Array("Sheet1", "Sheet2")).Copy 将自动为您创建一个带有新链接的新工作簿。这种方法有效吗?
  • SaveAs 的答案帮助我解决了同样的问题:我复制了新数据但保留了相同的公式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多