Consulting

Results 1 to 8 of 8

Thread: Solved: copy contents of one workbook into another

  1. #1

    Solved: copy contents of one workbook into another

    I have two workbooks. WorkbookA. WorkbookB. Both workbooks have sheets by the names of Sheet1, Sheet2, and Sheet3. I need a macro that does the following:

    *** Copy the contents of WorkbookA Sheet1 onto WorkbookB Sheet1
    *** Copy the contents of WorkbookA Sheet2 onto WorkbookB Sheet2
    *** Copy the contents of WorkbookA Sheet3 onto WorkbookB Sheet3

  2. #2
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location
    Hi joeyc,

    If you know the columns that you need to copy you can use something like:

    Private Sub CopyData(path1, path2)
    
    Set wb1 = Workbooks.Open(path1, True, True)
    Set wb2 = Workbooks.Open(path2, True, True)
    
    With ThisWorkbook.Worksheets("Sheet Name")
            .Columns("A:B").Value = wb1.Worksheets(Sheet Name).Columns("A:B").Value
            .Columns("D:E").Value = wb2.Worksheets(Sheet Name).Columns("A:B").Value
    End With
    
    End Sub
    Where the columns you wish to copy are separated by a semi-colon, and, path1 and path2 are the filepaths of the workbooks you wish to copy from.

    As for copying the whole workbook, im not sure.

    Gary

  3. #3

    Thanks!

    Let me take a look at this. I will be back.

    I am on Eastern Standard Time by the way.

  4. #4

    I'm confused

    That doesn't seem to work.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]
    Sub CopySheets()
    Dim Arr, a
    Arr = Array("Sheet1", "Sheet2", "Sheet3")
    For Each a In Arr
    Workbooks("Book1.xls").Sheets(a).Cells.Copy _
    Workbooks("Book2.xls").Sheets(a).Range("A1")
    Next
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Works perfectly.


  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A utlility I've been working on.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    I will check it out. Thank you for letting me know.

Posting Permissions

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