简体   繁体   中英

VBA copy sheet from a .XLSX and insert it into a .XLA file

I'm trying to copy a Sheet from a .XLSX file as a hidden sheet in a .XLA file.

I only managed to copy from .XLA -> XLSX , but not XLSX -> XLA .

Take a look at my two functions:

Sub copyFromXLAtoXLSX()
    temp.Sheet1.Copy Workbooks(1).Sheets(1)  
End Sub

Sub copyFromXLSXtoXLA()
    ' It gives me the error: Run-time error '1004': Application-defined or object-defined error  
    Workbooks(1).Sheets(1).Copy temp.ThisWorkbook.Sheets(1)  
End Sub

(左)copyFromXLAtoXLSX和(右)copyFromXLSXtoXLA

Any help is really welcome. Thanks!

The XLA sheets collection cannot be accessed for adding a new sheet as long as .IsAddIn=True . Try to set the XLA workbook to .isAddIn = False , make your copy and then set it back to True .

I think it should be (to be tested):

Sub copyFromXLSXtoXLA()
    Workbooks(1).IsAddIn = False '<-- set it false
    Workbooks(1).Sheets(1).Copy temp.ThisWorkbook.Sheets(1)  
    Workbooks(1).IsAddIn = True '<-- set it back to true
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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