Results 1 to 4 of 4

Thread: [RESOLVED] Copying workbooks into a single workbook

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Resolved [RESOLVED] Copying workbooks into a single workbook

    Hi
    I have an excel workbook "Statistics" containing 7 worksheets,and 7 workbooks containing data in their first sheet(in these workbooks there are data in Sheet1).I want to copy the content of these 7 workbooks into each sheet in "Statistics"
    e.g let's name the 7 workbooks as follow:
    Book1,book2,book3,book4,book5,book6,book7 and my workbook "Statistics" contains 7 worksheets:Sheet1,Sheet2,Sheet3,Sheet4,Sheet5,Sheet6,Sheet7.
    I want to copy the content of book1 into Statistics.Sheet1,the content of book2 into Statistics.Sheet2...the content of book7 into Statistics.Sheet7.How can I do it programmatically?
    thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Copying workbooks into a single workbook

    Moved to Office Development

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Copying workbooks into a single workbook

    Something like this should do it, assuming that the workbooks are all in the same folder. You will need to change the value fo the sPath constant to the folder address.
    VB Code:
    1. Sub CopyStats()
    2. Const sPath As String = "C:\data\"
    3.  
    4. Dim lCounter As Long
    5. Dim sBookName As String
    6. Dim wkbSource As Workbook
    7. Dim wksSource As Worksheet
    8. Dim wksTarget As Worksheet
    9.  
    10.     '7 books to copy
    11.     For lCounter = 1 To 7
    12.        
    13.         'Set the book name
    14.         sBookName = "Book" & CStr(lCounter)
    15.        
    16.         'Open the Workbook
    17.         Set wkbSource = Workbooks.Open(sPath & sBookName)
    18.        
    19.         'Reference the Source Page
    20.         Set wksSource = wkbSource.Worksheets("Sheet1")
    21.        
    22.         'Reference the Destination Page
    23.         Set wksTarget = ThisWorkbook.Worksheets("Sheet" & CStr(lCounter))
    24.        
    25.         'Copy the Source Page to the Destination Page
    26.         wksSource.Cells.Copy wksTarget.Cells
    27.        
    28.         'Clear the Worksheet object variables
    29.         Set wksSource = Nothing
    30.         Set wksTarget = Nothing
    31.        
    32.         'Close the Workbook
    33.         wkbSource.Close False
    34.        
    35.         'Clear the Workbook object variable
    36.         Set wkbSource = Nothing
    37.     Next lCounter
    38.  
    39. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: Copying workbooks into a single workbook

    Thanks DKenny a lot

Posting Permissions

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



Click Here to Expand Forum to Full Width