简体   繁体   English

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

[英]VBA - Copying worksheet from one workbook to another (values, not formulas)

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

I would like to use something like: 我想使用类似:

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

The problem is that some of the cells in sheet(1) of book1.xlsm have formulas. 问题在于book1.xlsm的sheet(1)中的某些单元格具有公式。 Using the second method pulls the formulas, and the resulting values are linked to the data in book2.xlsm 使用第二种方法提取公式,并将结果值链接到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

Note: both files need to be open in the same instance of the Excel application 注意:两个文件都需要在Excel应用程序的同一实例中打开

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

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