How to copy sheets from a workbook to another workbook

前端 未结 2 486
名媛妹妹
名媛妹妹 2021-01-28 04:40

I have this code to copy a sheet from a Workbook in VBA/Access to another Workbook/File.

Dim File1 as String
Dim File2 as String
File1 = \"D:\\File1.xls\"
File2          


        
2条回答
  •  后悔当初
    2021-01-28 04:57

    If you are running in MS Access, you need something on these lines:

    Dim CopyFrom As Object
    Dim CopyTo As Object ''Early binding: Workbook
    Dim CopyThis As Object
    Dim xl As Object ''Early binding: New Excel.Application
    
    ''Late binding
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
    
    ''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
    Set CopyFrom = xl.Workbooks.Open("z:\docs\From.xls")
    Set CopyThis = CopyFrom.Sheets(1) ''Sheet number 1
    Set CopyTo = xl.Workbooks.Open("z:\docs\To.xls")
    CopyThis.Copy After:=CopyTo.Sheets(CopyTo.Sheets.Count)
    
    CopyFrom.Close False
    

提交回复
热议问题