+ Reply to Thread
Results 1 to 12 of 12

Thread: How to copy data from one workbook to another

  1. #1
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Question How to copy data from one workbook to another

    Hi guys

    Hope someone can help? I have experience with excel but not the VBA and macro side of excel. Basically I have 10 Teams each with their own excel workbook used to record daily figures. The last sheet on each workbook has their running total for the month. All I want is a new workbook that can copy the monthly table from each teams workbook into the new workbook for comparison. Does anyone know if this can be done? I was also hoping to be able to have sheets rename themselves based on data in a cell on the sheet itself? The monthly total sheets name would remain the same.

    Thanks

    Mark
    Last edited by Gr8er1mark; 01-13-2012 at 01:58 PM. Reason: title change

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How to copy data from one workbook to another

    Here's some code that you can adapt for your needs

    Option Explicit
    '---------------------------------------------------------------------------------------
    ' Module    : Data
    ' Author    : Roy Cox (royUK)
    ' Website   : for more examples and Excel Consulting
    ' Date      : 19/11/2011
    ' Purpose   : Combine data from several workbooks
    ' Disclaimer: Disclaimer; This code is offered as is with no guarantees. You may use it in your
    '             projects but please leave this header intact.
     
    '---------------------------------------------------------------------------------------
    
    Option Explicit
    
    Sub CombineData()
        Dim oWbk As Workbook
        Dim uRng As Range
        Dim rToCopy As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
        Dim sFil As String
        Dim sPath As String
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            '   On Error GoTo exithandler
            sPath = ThisWorkbook.Path & Application.PathSeparator & "Data"
            ChDir sPath
            sFil = Dir("*.xls")    'change or add formats
            Do While sFil <> ""    'will start LOOP until all files in folder sPath have been looped through
    
                With ThisWorkbook.Worksheets(1)
                    Set uRng = .UsedRange
                    If uRng.Cells.Count = 0 Then
                        'no data in master sheet
                        bHeaders = False
                    Else: bHeaders = True
                    End If
    
                    Set oWbk = Workbooks.Open(sPath & Application.PathSeparator & sFil)    'opens the file
                    Set rToCopy = oWbk.ActiveSheet.UsedRange
                    If Not bHeaders Then
                        Set rNextCl = .Cells(1, 1)
                        bHeaders = True
                    Else: Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
                        'headers exist so don't copy
                        Set rToCopy = rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                                  rToCopy.Columns.Count)
                    End If
                    rToCopy.Copy rNextCl
                End With
                oWbk.Close False     'close source workbook
                sFil = Dir
            Loop    ' End of LOOP
            'sort to remove empty rows
            Set uRng = ThisWorkbook.Worksheets(1).UsedRange
            uRng.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
                      OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                      DataOption1:=xlSortNormal
    exithandler:
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Quote Originally Posted by royUK View Post
    Here's some code that you can adapt for your needs

    Option Explicit
    '---------------------------------------------------------------------------------------
    ' Module    : Data
    ' Author    : Roy Cox (royUK)
    ' Website   : for more examples and Excel Consulting
    ' Date      : 19/11/2011
    ' Purpose   : Combine data from several workbooks
    ' Disclaimer: Disclaimer; This code is offered as is with no guarantees. You may use it in your
    '             projects but please leave this header intact.
     
    '---------------------------------------------------------------------------------------
    
    Option Explicit
    
    Sub CombineData()
        Dim oWbk As Workbook
        Dim uRng As Range
        Dim rToCopy As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
        Dim sFil As String
        Dim sPath As String
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            '   On Error GoTo exithandler
            sPath = ThisWorkbook.Path & Application.PathSeparator & "Data"
            ChDir sPath
            sFil = Dir("*.xls")    'change or add formats
            Do While sFil <> ""    'will start LOOP until all files in folder sPath have been looped through
    
                With ThisWorkbook.Worksheets(1)
                    Set uRng = .UsedRange
                    If uRng.Cells.Count = 0 Then
                        'no data in master sheet
                        bHeaders = False
                    Else: bHeaders = True
                    End If
    
                    Set oWbk = Workbooks.Open(sPath & Application.PathSeparator & sFil)    'opens the file
                    Set rToCopy = oWbk.ActiveSheet.UsedRange
                    If Not bHeaders Then
                        Set rNextCl = .Cells(1, 1)
                        bHeaders = True
                    Else: Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
                        'headers exist so don't copy
                        Set rToCopy = rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                                  rToCopy.Columns.Count)
                    End If
                    rToCopy.Copy rNextCl
                End With
                oWbk.Close False     'close source workbook
                sFil = Dir
            Loop    ' End of LOOP
            'sort to remove empty rows
            Set uRng = ThisWorkbook.Worksheets(1).UsedRange
            uRng.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
                      OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                      DataOption1:=xlSortNormal
    exithandler:
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub
    Thanks for that. Unfotunately I'm a novice when it comes to script like this and wouldn't know where to start to adapt this to my needs?

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How to copy data from one workbook to another

    You need to do some reading on VBA then or stick to manually copying & pasting. With the information that you have provided you can only expect general help, we are here to help you learn, not provide custom code
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: How to copy data from one workbook to another

    This is a duplicate post. There is another post from the same person - http://www.excelforum.com/excel-prog...o-another.html.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How to copy data from one workbook to another

    Thanks Arlu.

    Gr8er1mark

    Don't waste our time with duplicate posts.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  7. #7
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Being new to forums and unsure of what is expected. You posted a message telling me the title was worng and did not comply. I changed the title and understood it that the original post was stopped and therefore re-posted it with a new heading. I am certainly not out to "waste time" and appreciate any help I can get.

  8. #8
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Hi

    I have been playing around with the sample code that you posted. But seem to be stuck. When I run it I get "Path Not Found" I think what I understand is that "sPath" set the directory to the directory my excel file is in? But even if I manual enter the directory after chDir (Instead of sPath) I still get the same result? Is there something I'm not doing?

    Thanks

    Mark

    P.S How do I stop one of the threads seeing as there is now two?

  9. #9
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Sorry forget the last bit. I noticed "& Data" that was being added to the path so I took it out. Still trying to work the rest out as seems to run with no errors but nothing copies over.

  10. #10
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How to copy data from one workbook to another

    I've closed the other Thread.

    As the code is set up now the files to copy from are ina sub folder of the folder that contains the master file. The sub folder is called Data
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  11. #11
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Hi again

    Got it working! Thank you. I took my sheet home from work home p.c uses excel 2010 as work p.c uses 2003 that wouldn`t make a difference would it? Only thing is it copies every sheet from every workbook I would like it to copy only one sheet from each called "Work Totals" is that possible? Thanks for your help so far ive been racking my brain with this for weeks.

  12. #12
    Registered User
    Join Date
    01-12-2012
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to copy data from one workbook to another

    Managed to get it sorted to take data from sheet 22 of the workbooks and it was working but now I am getting a runtime error 1004 "set method of range class failed" Any idea what the problem could be?

    Thanks

    Mark

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0