+ Reply to Thread
Results 1 to 36 of 36

Help on how to Compare five Excel workbooks and copying matched data to a new workbook

  1. #1
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Question Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Hello,
    I am new to excel and need help please, I'm trying to compare five workbooks to each other in excel and have matching column data copy the entire rows into a new workbook (ReportWorkBook). Ex:

    Compare Workbook_1 column A to Workbook_2 column A to Workbook_3 to Workbook_4 column A to Workbook_5 column A

    If there is names matched, copy the entire 5 rows ( one row from each workbook for the same name if match found in all of them) of matching Workbook_1, Workbook_2, Workbook_3, Workbook_4, Workbook_5 data to the new Workbook (ReportWorkBook). Also to make each name data rows separated from others, meaning if there are more than 1 name matched in all of the workbooks, each name data bulk to appear as a separate table.


    Any help would be appreciated.
    Thanks

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Welcome to the forum


    Can you clarify:

    1. Do you want each block of 5 rows to have headers?
    2. Should each block of 5 rows be separated by a blank row?
    3. Does a name in columnA only appear once in any worksheet?

    thanks
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Hi,
    I think you should separate your task:
    1) Collect all data from all 5 workbooks into one workbook. You can search for this task on this forum or others, it should be a lot of posts and solutions on this topic
    2) It's not really clear what excactly do you need to compare. If you need to match all names only from Workbook_1 then it's one logic.
    Perhaps you need to combine all names from all workbooks, I mean in case Workbook_1 contains 5 names and Workbook_2 contains 15 names, I guess you need to check all names and find matches, right?
    If so, then you first of all should collect all unique names and then check each this name in all workbooks. This is about VBA solution.
    But you can try to do this even witout VBA:
    1) Copy-Paste all data from every workbook ot one workbook (copy each table and make one big table)
    2) Then use COUNTIF formula, apply it to all names and you will get count of each name in all your data
    3) If this count for some name is 5 - this means that current name is represented in each of 5 workbooks
    4) Filter out rows with names, where count is 5 and get your result.

    In case you wish to get some real help with VBA or formulas please attach some sample file with little amount of data

  4. #4
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    1. Yes with headers.
    2. Each block of 5 rows be separated by white space (As a separate table), eg:
    Tables_Image.PNG
    3. Yes the name appear only once per sheet if found.

    Thanks

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Try this:
    - Put attached workbook in same folder as the 5 files
    - Amend workbook names in this line (enclose name in " ", and do not include extension) :
    Please Login or Register  to view this content.
    - run with {CTRL} t

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Can you please help me with this two lines:

    myPath = ThisWorkbook.Path & "\" 'amend to suit' / Which workbook path and where to put the path before the & or inside "\" ?


    'create new file
    newFileName = "MyFileName" & Format(Now, "hh mm ss") 'amend to suit' / What exactly do I need to amend?

    Thanks

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Q1 where do you want to save the new file? What is the full path? It is currently set to save to same folder as Master file - so you can leave it as it is if you want.

    So if you want to save it to C:\Documents\Excel
    Please Login or Register  to view this content.
    Note - the code assumes the other 5 workbooks are in the same folder - so you will need to move them or amend the code so that VBA knows where to find them
    Last edited by kev_; 07-07-2017 at 04:40 PM.

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Q2 what do you want to call the new workbook?
    If you want to name it Consol then
    Please Login or Register  to view this content.
    The bit after the "&" is a timestamp -so you can ignore that if you want
    The file automatically receives extension .xlsx when saved
    Last edited by kev_; 07-07-2017 at 04:38 PM.

  9. #9
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    After I made the changes It keeps highlighting the following line:

    wbNew.SaveAs (myPath & "\" & newFileName) but there is no "amend to suit" next to this line, do I need to change anything here?

  10. #10
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    No -the problem is probably elsewhere, but ....
    - you could try putting in this line BEFORE that line and see if the message box tells you something - is the path valid?
    Please Login or Register  to view this content.
    If that does not provide any clues:
    I suggest we go one stage back and let's prove that the code works for you BEFORE you adjust to match your folder structure
    So go back to my original code
    Amend only the file names
    Create a test folder and put a copy of each of the 5 files there
    Also put Master.xlsm in that folder
    Next, amend this line to match the 5 file names
    Please Login or Register  to view this content.
    Please confirm that the 5 files have extensions .xlsx ( this is what the code is expecting)

    If the code still breaks down at the same line, then there must be a minor difference between Excel 2007 (Is that what you are using?) and Excel 2016 - I will make a modification tomorrow if that is the case

    Let me know if it worked or not - I will be back in the morning
    If your code does not work, please post your code in full enclosed in code tags - click on # icon and
    [ CODE]code here[ /CODE]
    Last edited by kev_; 07-07-2017 at 05:41 PM.

  11. #11
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Hi,
    1. I used the original code amending only the line wbNamesArr = Array("F1", "F2", "F3", "F4", "F5"), but it's given me the following error:
    and when I click on debug, The following line is highlighted.


    Set wb = Workbooks.Open(wbName)

    2. I put the code MsgBox myPath & "\" & newFileName before the line wbNew.SaveAs (myPath & "\" & newFileName) but it's showing the same error.
    Last edited by net1media; 07-08-2017 at 05:00 AM.

  12. #12
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    as requested in post#10
    if your code does not work, please post your code in full enclosed in code tags - click on # icon and
    [ CODE ]paste code here[ /CODE ]
    Last edited by kev_; 07-08-2017 at 05:07 AM.

  13. #13
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Does the file Arbalet2.xlsx exist in that folder ?
    Is it Arbalet2.xls perhaps ?

  14. #14
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I have already changed the file name, and saved it, but I don't know if it's this file or it's the link to it from the module.

  16. #16
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Yes the extensions is .xlsx

  17. #17
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I found the problem in the wbNamesArr = Array("F1", "F2", "F3", "F4", "F5") in the "F1" Link because I swapped the name Arbalet2.xls with List_1 and the same error coming up with List_1 instead.

  18. #18
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    You are confusing me totally here

    1. What are the 5 file names
    2. What is the full folder path?

  19. #19
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    1. The files names are "List_1", "Arba_let2", "Expedia1", "Expedia2", "Book3"
    2. The folder path is: Documents\HR_ProfileBooks

    Any file name I put in the "F1" section, the error message appear that the file is not found, So I think it's the "F1" code link that causing the problem not the file itself.

  20. #20
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I created new folder C:\Documents\HR_ProfileBooks
    I put Master.xlsm and the 5 files in that folder
    I amended the code to match
    I ran the code
    It works without any issues

    - 2 lines only amended in the code
    Please Login or Register  to view this content.

    My files have 2 columns A with Nmaes and B with file name, only the first 2 names are present in all files

    Result
    combine5files01.jpg


    Please compare the code below and tell me what was different in your code - it should match EXACTLY


    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I checked it, There are 2 lines not in my code:
    Sub CopyRowsIfNameAppears5Times()
    Please Login or Register  to view this content.
    I added them, but is still not working, I am using Excel 2007, Would this be the problem?

  22. #22
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Those lines are comments only - so are not part of the code (- anything starting with apostrophe is a comment)

    Try simply using my code (per post#20) instead of yours
    (There may something small you are missing)

    Let me know how you get on
    Last edited by kev_; 07-08-2017 at 10:09 AM.

  23. #23
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    The code now is running without any errors, but the report file only copying the headers rows (Columns Title) without any matched name or any name data (Row) .

    Result.PNG
    Last edited by net1media; 07-08-2017 at 04:47 PM.

  24. #24
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    The next thing to do is to stop the code running after all values have been copied to new workbook and look at those values.
    So amend the code:
    Before the line that begins 'Remove all rows where count of names.... insert this line
    Please Login or Register  to view this content.
    Column A contains the CountIf formula - for every name that appears in all 5 files there should be the value 5 in column A. Every row without 5 in column A is subsequently deleted - so this is most likely where the problem lies.

    Let me know what you find...
    (All values from all files should be there with the headers appearing once only)
    Last edited by kev_; 07-09-2017 at 02:49 AM.

  25. #25
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    1. Every name might not appears in all 5 files, So I don't want it to show the name if it appear 5 times exactly, but from 2 to 5 times, so if found in 2 or more workbooks it should copy the rows.
    2. Can I add the workbook name to each row from where it was copied and make borders, Eg:

    Result.PNG

  26. #26
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Here are the 5 files that I used to test - there's most likely a minor difference between the layout of your files and mine
    (or Excel 2007 is behaving differently to Excel 2016 somewhere - I do not think that is what it is because all the commands & functions used were present in Excel 2007)

    I suggest you:
    - create a new test folder and put my test files in that folder
    - leave Master.xlsm in its original folder
    - amend the path in the vba to match the new folder
    - remove the "Exit Sub" line if it is still in there per my instruction in post#24
    - run the macro - it should work
    - if it does not work then there must be a minor difference somewhere between 2007 and 2016

    The next step is to work out (together) what to change
    Are you available at 12 noon today? and then we can be online at same time and I will tell you exactly how to test each stage


    For info - this is what the macro does step by step:
    creates a new file

    opens file "List_1"
    adds header row to new file (values placed in row 1)
    copies sheet values (rows 2 to last row with value in column A)
    pastes to new file to first empty row below existing data (again based on columnA) - in this case it will be row 2
    closes "List_1"

    opens "Arba_let2"
    copies sheet values (rows 2 to last row with value in column A)
    pastes to new file to first empty row below existing data (again based on columnA) - in this case it will be row 2
    closes "Arba_let2"

    same as above for "Expedia1" "Expedia2" "Book3"

    inserts temporary working columns (A & B)
    insert temporary formulas
    columnA COUNTIF(C2:C999,C2) - where 999 is the last used row
    columnB contains a sequential number - this actually isn't being used - I thought it may have been required for sorting but I did not need to use it

    removes all rows where count of names is not equal to 5
    - uses autofilter and looks at values in columnA
    - all rows where values in column A not equal to 5 are displayed
    - all visible rows are deleted
    - autofilter set to off which displays all remaining values (ie those equal to 5 in column A)

    sorts data by name

    inserts header for each "name" and a blank row in between each name block

    deletes temporary columns
    Attached Files Attached Files

  27. #27
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Ignore my last post for the moment - our post have crossed - will get back to you in 30 minutes

  28. #28
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    This problem was caused by your first post - these are your words - you said ALL of the files

    one row from each workbook for the same name if match found in all of them
    Will send you revised code in 30 minutes......

  29. #29
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Sorry for the wrong explanation, It's minimum 2 match up to 5.

  30. #30
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Use the full code below and amend newFileName value to whatever you are using

    Amendments made to code (with explanations):

    1. this line speeds up the running of the code - screen is now only updated at the end of the macro when the same property is reverted to true
    Please Login or Register  to view this content.
    2. this filters to show only those rows where the name count = 1 (these rows are subsequently deleted)
    Please Login or Register  to view this content.
    3. this bit of code:
    - starts with the last row and
    - works backwards through name values (names are currently in column C due to temporary formulas)
    - blank row and headers inserted if name differs from name above
    - does not need to do that at row 2 because the header is already there (Exit For tells vba to exit the current For loop)
    - where possible always step backwards through the rows when deleting or inserting rows
    (if delete/insert starting at top then row numbers below are changed each time and that would have to be built into the VBA - "i" would require resetting)
    Please Login or Register  to view this content.
    full code:
    Please Login or Register  to view this content.

  31. #31
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    After testing the above code AND getting it to work, then to add in the borders

    place the code below immediately below this line:
    Please Login or Register  to view this content.

    The code works as follows:
    c = last column
    check to see if there is anything in each cell in column A If Not IsEmpty(.Cells(r, 1)) Then-
    put border around cells in columns 1 to c in that row

    code:
    Please Login or Register  to view this content.

  32. #32
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I edited the code but this is how the page looks

    Result1.PNG Result2.PNG.

    Also no source file name anywhere like my post 25
    Last edited by net1media; 07-09-2017 at 09:07 AM.

  33. #33
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I spotted a small error in the code

    Please Login or Register  to view this content.
    should be
    Please Login or Register  to view this content.
    Please amend it and test again
    thanks

  34. #34
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    I put the code for the border and added
    Please Login or Register  to view this content.
    but I still have a problem with the page:

    Result3.PNG
    Last edited by net1media; 07-09-2017 at 10:59 AM.

  35. #35
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Please attach your 5 files - I will get the code to work with your files

  36. #36
    Registered User
    Join Date
    07-06-2017
    Location
    UK
    MS-Off Ver
    2007
    Posts
    17

    Re: Help on how to Compare five Excel workbooks and copying matched data to a new workbook

    Sorted, Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Compare two excel workbooks (or two worksheets within a single workbook)
    By sergiozygmunt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2016, 03:11 AM
  2. Copying data from multiple workbooks to 1 master workbook
    By chilli76 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2014, 12:50 PM
  3. Replies: 1
    Last Post: 01-07-2014, 09:09 PM
  4. Compare Data between 2 Workbooks & Copy Add'l Data If Matched
    By Marcieface in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-15-2013, 09:05 PM
  5. Replies: 2
    Last Post: 01-27-2012, 02:19 AM
  6. Replies: 1
    Last Post: 04-19-2010, 03:26 PM
  7. Compare two Workbooks and combine data into one workbook
    By SBonner in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-12-2006, 08:25 PM

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