Results 1 to 5 of 5
  1. #1
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70

    Copying Excel worksheets from one workbook to another

    Hi all,
    I am trying to copy a worksheet in one Excel workbook, to another Excel workbook. There is only 1 sheet in the source workbook that needs to be copied.

    I found some examples in other forums, but I receive runtime error 9 - Subscript out of range when I run the code.

    Code below:



    Code:
    Dim CopyFromBook As WorkbookDim CopyToBook As Workbook
    Dim ShToCopy As Worksheet
    
    
    Set CopyToBook = Workbooks("c:\temp\WF_Conversion_08-Mar-2023.XLSX")
    For Each ShToCopy In Workbooks("c:\temp\WF_Conversion_BackPay_08-Mar-2023.XLSX").Worksheets
        ShToCopy.Copy After:=CopyToBook.Sheets(CopyToBook.Sheets.Count)
    Next ShToCopy
    The error occurs in the "SET CopyToBook...." statement.
    Any suggestions in what I am doing wrong?
    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Where are you running this code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70
    I am running the code from within a module.
    I am calling the sub from the main form.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    So you are running in Access?
    Code:
    Sub CopySht()
    Dim xl As Excel.Application
    Dim wbMaster As Excel.Workbook
    Set xl = New Excel.Application
    Set wbMaster = xl.Workbooks.Open("c:\temp\WF_Conversion_BackPay_08-Mar-2023.XLSX")
    With xl.Workbooks.Open("c:\temp\WF_Conversion_08-Mar-2023.XLSX")
        wbMaster.worksheets(1).Copy After:=.worksheets(.worksheets.Count)
        .Close True
    End With
    wbMaster.Close
    xl.Quit
    End Sub
    Based on code in https://stackoverflow.com/questions/...s-in-ms-access
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70
    Thanks June7 - resolved!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-21-2018, 07:32 PM
  2. Replies: 2
    Last Post: 08-28-2015, 01:23 PM
  3. Importing Excel Workbook with multiple Worksheets
    By MTSPEER in forum Programming
    Replies: 4
    Last Post: 04-21-2015, 01:50 PM
  4. New workbook/worksheets using Transfer Spreadsheet
    By nyneave in forum Import/Export Data
    Replies: 3
    Last Post: 09-05-2012, 06:44 PM
  5. Deleting Worksheets on a Excel workbook
    By BED in forum Programming
    Replies: 0
    Last Post: 07-27-2010, 01:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums