简体   繁体   中英

Excel - Copy between two workbooks in VBA

I have two workbooks in excel. I am trying to copy a worksheet from one workbook to another. And after that I want to close the workbook where I had copied from. What I have done so far:

Sub copy() Workbooks.Open filename:= _ "C:\\2016.xlsm" ActiveWorkbook.Sheets("Grafic").Select Selection.Copy Destination:=Workbooks("C:\\Grafic.xlsx").Sheets("Sheet1").Range("A1") End Sub

Thanks.

Maybe this helps

Option Explicit

Sub CopyIt()

Dim wb As Workbook
Dim copyWb As Workbook
Dim wks As Worksheet
Dim fileName As String, sheetName As String

    fileName = "... complete filename ..."
    sheetName = "... sheet name ..."

    Set wb = Workbooks.Open(fileName:=fileName)
    Set wks = wb.Sheets(sheetName)

    Set copyWb = ThisWorkbook ' the workbook you would like to copy to
    wks.copy before:=copyWb.Sheets(1)

    wb.Close False
End Sub

Use

Application.Workbooks("2016.xlsm").Close

Close method has some parameters to set if you want to save changes or not.

More info:

Workbook.Close

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