+ Reply to Thread
Results 1 to 25 of 25

VBA to copy data from multiple workbooks to Master Workbook

  1. #1
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    VBA to copy data from multiple workbooks to Master Workbook

    I have 4 workbooks which each contain a sales log. 1 is a Master Log and 3 are maintained by separate users. I am looking for the easiest way to update the Master Log with the data from the other 3 workbooks that is fast, user friendly and avoids data being corrupted by the users.

    Open to suggestions other than the following, however if this is an appropriate solution would appreciate the help in writing the VBA...

    Attached is a sample of how the data is ordered -- I have considered using named ranges and running a macro in the Master Log that would:-
    1. Open Sales Person 1 Workbook
    2. Copy Range1
    3. Paste Range1 data (values only) in Master Log Range1
    4. Close Sales Person 1 Workbook
    5. Repeat for Sales Person 2 and 3 Workbook
    6. Save and close Master Log

    Will also need to consider ensuring each worksheet is sorted. All sheets are named the same in each workbook.

    Thanks in advance.
    Master Log.xlsx
    Sales Person 1.xlsx
    Sales Person 2.xlsx
    Sales Person 3.xlsx
    Last edited by wotsup; 02-17-2012 at 11:39 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Should the data be copied one below the other in the Master workbook or in separate sheets? If its the former, should there be a separate column stating which book the data was copied from?
    If I have 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]

  3. #3
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Yes... one below the other. I have allowed for 1000 rows for each data set. The separate column stating which book the data is from is a good idea.

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

    Re: VBA to copy data from multiple workbooks to Master Workbook

    I checked the sales person's files. Will the data always be located in those rows? Or would they each start from row 3?

  5. #5
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    They will always be in those rows. This is why I have named each set as a range and numbered column B to enable sorting them in order. Unless there is a better way?

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    You do not need to use named ranges here. There is a faster, easier way of doing it through code. The sales person's can start entering their values just below the header, that is from row 3 onwards. The code will pick it up automatically.

    Is there a particular reason why you need to sort it in the order of column B?

  7. #7
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Periodically the Master Log is distributed to each sales person. This becomes their new data set. I have included a macro to sort their specific data to make it easy for them to view their own data and not accidentally overwrite someone elses. This is the only reason I have included column B.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    You can continue with column B, what you can do is have the numbers for the 2nd sales person start in row 3 with number 2 and then row 4 will have 2.01. This will then ensure that the macro works smoothly.

    I will provide you the code shortly.

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    You can use this code. It also includes the sort option.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Thanks... we are definitely on the right track.

    What if I only want to copy the data in each Sales Persons Log that pertains to them? Remember, as the Master Log is updated, each Sales Person receives a copy and begins updating their own data. I have a lookup for each Sales Person in Column H based on their corresponding client. Could this be used? Another idea was to call up a range.

    Thanks in advance.

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Do you mean that the Sales Persons log will also have entries that belong to the other sales people?

  12. #12
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Yes, that's correct.

  13. #13
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    In that case, the code will need to be modified a bit. Will there always be 10 rows per person or is it variable?

  14. #14
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    There is actually 1000 rows per person.

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Use this updated code -
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    I think we're almost there....

    Instead of the data pasting at the bottom of the Sales Log in the Master File, what can be done to paste it in the corresponding location to the Sales Person File i.e. Sales Person 1 .Range("B3:E1002").Paste. Otherwise the current code will create duplicates.

  17. #17
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Use this code -
    Please Login or Register  to view this content.
    You can retain the sort code or remove it if not required.

  18. #18
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Perfect! One last request. Now that the Master File is fully updated, can we write some code that will Save each of the new Sales Person files by overwriting the old ones? i.e. File/Save As/Sales Person 1 etc?

  19. #19
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Yes, sure. Can you provide the path where the files can be saved? Or do you want the user to select that?

  20. #20
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    It will be the same path as where the files were originally located. So lets go with D:\Test as in your sample above. Is this ok?

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    If you are not using the Sort code, then after this line
    Please Login or Register  to view this content.
    put this code -
    Please Login or Register  to view this content.
    This will resave the master file thrice with the new names.

  22. #22
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Ok

    Ok... I have modified the last piece to cater for the Macro Enabled Workbook as follows. The only problem is that the Master File is Closing and the file left open is Sales Person 3. Any ideas?

    ActiveWorkbook.SaveAs Filename:="D:\Test\Sales Person 3.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="D:\Test\Sales Person 3.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="D:\Test\Sales Person 3.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

  23. #23
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Why do you want to save the sales person files as macro enabled? The macro would be residing in the master file wouldnt it?

    You can do this - even before the save as code, put this line
    Please Login or Register  to view this content.
    This will save your Master file wherever you have saved it initially. It will then saveas to the 3 new names.

  24. #24
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Works Brilliant. Thanks for your patience... You're a champion!

  25. #25
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Np at all. Glad it worked. If your issue has been solved, please edit your original post and select go advanced, then select the prefix - Solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1