Results 1 to 4 of 4

Thread: VBA Copy Worksheet to Another Workbook
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular Small Paul's Avatar
    Join Date
    Jun 2018
    Location
    Wokingham, UK
    Posts
    118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Copy Worksheet to Another Workbook

    Hi

    I have 2 workbooks. Workbook A contains 1 worksheet. Workbook B (purchased items) has a variable number of worksheets.

    I need to copy the worksheet in A and add it to be the LAST worksheet in B.

    The code I have at present is:



    Code:
    Sub Macro1()'
    ' Macro1 Macro
    '
    activesheet.name = "Payee Details"
    activesheet.Copy
    after = Workbooks("Purchased Items.xlsm").worksheets.Count
    
    
    End Sub
    When run, the name is changed and the worksheet is copied.

    However, it does not appear in 'Purchased Items'

    I know it is something simple but can somebody please help?

    Cheers
    Small paul.

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,220
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: VBA Copy Worksheet to Another Workbook

    If the code is in Workbook A try
    Code:
    Sub SmallPaul()
       Dim Wbk As Workbook
       
       Set Wbk = Workbooks("Purchased Items.xlsm")
       With ThisWorkbook.Sheets(1)
          .Name = "Payee details"
          .Copy , Wbk.Sheets(Wbk.Sheets.Count)
       End With
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    Board Regular Small Paul's Avatar
    Join Date
    Jun 2018
    Location
    Wokingham, UK
    Posts
    118
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Copy Worksheet to Another Workbook

    I keep ALL my code in 'Personal.xlsx'
    The above did not work, even when adjusted for the location.
    I now have the following which fails on the final line "Run-time error 1004: Copy method of Worksheet class failed

    Code:
    Sub Macro3()'
    ' Macro3 Macro
    '
       ActiveWorkbook.SaveAs "Z:\Paul\Payee Details.xls"
           activesheet.name = "Payee Details"
           worksheets("Payee Details").Select
       Workbooks("Payee Details.xls").worksheets("Payee Details").Copy , after = Workbooks("Purchased Items.xlsm").worksheets.Count
    
    
    End Sub

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,220
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: VBA Copy Worksheet to Another Workbook

    In that case, change ThisWorkbook to
    Code:
    Workbooks("Aname.xlsm")
    Last edited by Fluff; Jun 18th, 2019 at 12:20 PM.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •