且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA可以将值从封闭的工作簿复制并粘贴到活动的工作簿Mac OS X

更新时间:2022-12-12 08:22:09

您应该像这样进行真正的粘贴

You should do a real copy-paste like that

Sub ImportData()
    Dim App As New Excel.Application 'create a new (hidden) Excel

    ' remember active sheet
    Dim wsActive As Worksheet
    Set wsActive = ThisWorkbook.ActiveSheet

    ' open the import workbook in new Excel (as read only)
    Dim wbImport As Workbook
    Set wbImport = App.Workbooks.Open(Filename:="C:\History.xlsm", UpdateLinks:=True, ReadOnly:=True)

    'copy the data of the import sheet
    wbImport.Worksheets("AllDATA").Range("A1:HW6000").Copy
    wsActive.Range("A1").PasteSpecial Paste:=xlPasteFormats 'paste formats
    wsActive.Range("A1").PasteSpecial Paste:=xlPasteValues  'paste values

    App.CutCopyMode = False 'clear clipboard (prevents asking when wb is closed)
    wbImport.Close SaveChanges:=False 'close wb without saving
    App.Quit 'quit the hidden Excel
End Sub