VBA-将工作表从一个工作簿复制到另一个工作簿(值,而不是公式)

问题描述:

当前,我正在将工作表的值和格式复制到另一个工作簿中的工作表中,如下所示:

Currently, I am copying the values and formats of a worksheet to a worksheet in another workbook as such:

workbooks("book1.xlsm").sheets(1).range(someRange).copy
with workbooks("book2.xlsm").sheets(1).range("A1")
    .pasteSpecial xlPasteValues
    .pasteSpecial xlPasteFormats
end with

我想使用类似的东西:

workbooks("book1.xlsm").sheet(1).copy after:=workbooks("book2.xlsm").sheet(1)

问题在于book1.xlsm的sheet(1)中的某些单元格具有公式.使用第二种方法提取公式,并将结果值链接到book2.xlsm中的数据

The problem is that some of the cells in sheet(1) of book1.xlsm have formulas. Using the second method pulls the formulas, and the resulting values are linked to the data in book2.xlsm

如何使用第二种方法,但是要复制值而不是公式?

How can I use the second method, but have the values, not formulas, copied?

如果不可能的话,除了我目前正在使用的第一种方法以外,还有哪些替代方法?

If not possible, what are some alternatives, aside from the first method, which I am currently using?

您可以在复制操作后断开链接:

You can break the links after the copy operation:

Option Explicit

Sub copySheetValues()

    With Workbooks("Book2")

        Workbooks("Book1").Worksheets(1).Copy After:=.Worksheets(1)

        Application.Wait Now + TimeValue("0:00:01") 'built-in replacement for "Sleep"

        .BreakLink Name:=Workbooks("Book1.xlsm").FullName, Type:=xlLinkTypeExcelLinks

    End With

End Sub


注意:两个文件都需要在Excel应用程序的同一实例中打开


Note: both files need to be open in the same instance of the Excel application