+ Reply to Thread
Results 1 to 17 of 17

Compare two text files using vba arrays

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Compare two text files using vba arrays

    Hello everyone
    I have two text files and I need to compare between them

    I have a text file which each line has some words/abbreviations. (1st file)
    I want this file to be compared with another file which has another list of words. (2nd file)

    Go through each line of 1st and see if any words/abbreviations on that particular line has a match in the 2nd file. If at least one of the words/abbreviations has a match, copy that line to a new 3rd text file.
    If that row has no matches on the 2nd file, copy that line to a new 4th text file.

    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

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

    Re: Compare two text files using vba arrays

    Hi,
    Are your real files contains this kind a rows like attached files? You mentioned words/abbreviations in each file, but looks like it's just arrays of symbols.
    Is there any logic how words/abbreviations can be split in each line? To be more specific - do you expect that "ASHTO" will be recognized as match in first lines of both files?

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    Thanks a lot for reply. To see the text files better, you should open them using Notepad Plus Plus ..
    It is similar to lists

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    Any help in this topic please

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

    Re: Compare two text files using vba arrays

    I've done something like this, but with two excel sheets.
    First thing I did - created Scripting.Dictionary and filled it with all words/abbreviations from file1. Thats why I asked about how does words/abb seperated in your files. Words extraction from line can be done with Split().
    Then I took first line from file2 and loop throgh all Dictionary records and with Instr() found out if any words from file1 are in this line. In your case at the end of this loop (for first line of file2) - line should be copied to File3 (if found match) or File4 (no match).
    And this way throgh all lines in file2.
    If you think logic like this could help you - let me know, I'll share my excel-compare file.

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Compare two text files using vba arrays

    Maybe VBA is not the best tool for this job. This wheel has already been invented in Unix - Windows versions of the command are available. https://www.gnu.org/software/diffutils/
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    Thanks a lot for replies
    @Mr. Kasan thanks a lot for your idea. It will be better to have your excel file. May be then we can find a way to import the data from text files into excel and then export later
    @Mr. dflak thanks for reply . I will give it a try

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

    Re: Compare two text files using vba arrays

    File to compare two excel sheets (in different files) with identical sheet structure. This one does two things:
    1) Change color of cell if File1.Cell <> File2.Cell
    2) Change color of specific changed line in a cell (in case cell contains multi-line text).
    May be will be useful.

    I tried to read your text files and compare them.
    Split was made only by "vbLf" - thats mean for compare I used "A3", "A3 Thinking" ... from File1. If you need to compare "A3", "Thinking" - then another split is needed.
    Anyway, test it.

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

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    Thank you very much for your help and great reply
    I tried to adapt it to suit my text files and get them work but couldn't ..

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

    Re: Compare two text files using vba arrays

    I hope you didn't try to read your files with attached file? This one is for excel files only.
    Try to test code from post #8.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    But excel reads these text files in your post #8 in proper way I think and it appears in proper structure in column A for each file ...

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

    Re: Compare two text files using vba arrays

    Try this file. You need to change paths to all files in code.
    Just one correction in code from #8 - variable flag should be 0 for each step by i.
    Test it.

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

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    Thanks a lot for this code and I think it is too closer >>>
    I will attach simple text files to test on so as to be more clearer

    In the first text file there are five items:
    Yasser
    Ahmed
    Reda
    Khalil
    Laila

    And in second text file there are four items for exmaple :
    Ahmed
    Khalil
    Salah
    Yasser

    The 3rd text file would contain items that has match in 2nd file so the the 3rd text file would contain :
    Yasser
    Ahmed
    Khalil

    The 4th text file would contain the items that has no match so the result would be :
    Reda
    Laila

    Thank you very much
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    When testing post #12 the 3rd text file contained :
    Yasser
    Khalil

    While the 4th file
    Ahmed
    Reda
    Laila

    So the item Ahmed is missing from 3rd text file and existed in 4th text file (that's the only fault now)

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

    Re: Compare two text files using vba arrays

    I got it, my bad, index in array starts with 0, not 1
    Please change this line in last code (post #12):
    Please Login or Register  to view this content.
    to this line:
    Please Login or Register  to view this content.
    Worked for me on your last sample files.

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Compare two text files using vba arrays

    I was thinking of changing it but the electricity was off at this moment. You got it and thank you very very much for great help in this issue
    Best and kind regards

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

    Re: Compare two text files using vba arrays

    Thanks for feedback & reputation points

+ 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. [SOLVED] Compare two Text files : Multiple Look Up criteria
    By Hang Glider in forum Excel General
    Replies: 2
    Last Post: 04-15-2015, 11:05 AM
  2. Compare two arrays
    By Regenbui in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-19-2014, 03:35 PM
  3. Excel Macro to compare contents of text files in two folders which has same name
    By vanidirossi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-24-2014, 10:19 AM
  4. How to compare text files in excel (data pulled in with VLOOKUP)
    By brewcitybiz in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-15-2014, 06:05 AM
  5. vba compare arrays and remove exact matching arrays
    By jacojvv in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-25-2013, 07:30 AM
  6. Compare 2 Arrays
    By Antnee in forum Excel General
    Replies: 8
    Last Post: 12-22-2009, 08:24 AM
  7. Compare 4 text files using Macro
    By vijay2482 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2009, 07:19 AM

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