+ Reply to Thread
Results 1 to 25 of 25

Search & return all rows contains that word from all txt files with filename

  1. #1
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Search & return all rows contains that word from all txt files with filename

    Hi,

    I am not good in vba. I have about 500 txt files to search for the first word "subject" and return that whole sentence from each txt by filename. In each file can have more than 9 words with subject.

    If found the word subject, I need the whole sentence merged in 1 excel file.

    I was searching for the vba to meet this criteria but couldn't find in google.

    Can anyone please assist? Your assistance is very much appreciated.
    Attached Files Attached Files

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

    Re: Search & return all rows contains that word from all txt files with filename

    Try this code
    Please Login or Register  to view this content.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi,
    Wow it's working. However, if I have 5 Certificates, Output Result shows 7 Certificates where it take the last 2 rows of Cert4 and Cert5 columns extra repeated at Cert6 and Cert7 columns.
    If I have 8 Certificates column, output show 3 rows extra by taking Cert6, Cert7 and Cert8 repeated at Cert9, Cert10 and Cert11.

    Attached is the test results.

    Regards
    Chitra
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    Different method
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,

    Test result is correct now. It's not repeated. Thank you.

    However YasserKhalil vba format is correct. Need the format in column format, not in row
    I did a attached test result manually just to show the record is repeated.

    Regards
    Chitra

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    Then you will need to upload a workbook with your exact desired result, not the workbook from the other.

    PS.
    OK, I downloaded your original file.

    What do the numbers like 1,2,3... in the first row represent?
    Do they correspond to "Certificate" number?
    Last edited by jindon; 10-01-2018 at 12:48 AM.

  7. #7
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon
    Yes, it correspond to "Certificate number.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    Try this one
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,
    I love it. It works perfectly.

    If there is a file with no certificate, also need to display the filename with blank cell or state as none.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    Move one line in bold
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,

    I commented 'n = n + 1: Cells(n, 1).Value = fn but it's not working. Cert number heading and 1st filename is mssing but has data is there and the txt file with no certificate did not show.


    Please Login or Register  to view this content.
    Last edited by suchetherrah; 10-01-2018 at 05:36 AM.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    1)
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    2)
    Not to comment out, but MOVE.
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,

    The file without certificate shows data which is repeated from the next file contents.
    Attached is the test results and the contents of the data and the txt file looks like

    Chitra
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    When the txt file doesn't have "Certificate #", it adds a blank row, regardless of the line(s) of "Subject:", because it can not be lined up with the "Certificate #".

    How do you want really it?

  15. #15
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,
    Just a filename at column A. Column B onwards is blank row of cells for that filename.
    As you can see the Test.xlsm, it output the data for that file.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    By the way, you need to follow the forum rule and edit your post #11 first to continue.
    It is a strict rule here.

  17. #17
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,
    Done as requested.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    OK, thanks.

    If you just want the lines that begins with "Subject:" then
    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,

    When run the command getting error message, run time error '62' Input past end of file

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    You must have empty file in the folder.
    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,

    Meaning each time, I have to add the VBA and execute the vba?
    I am doing this for another department.

    Can I fix an empty file with macro set in it? I will be using the same empty filename to execute. The user will just have to click the macro button and it will overwrite.

    Error message run time error '62' Input past end of file hit at
    x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(myDir & fn).ReadAll, vbCrLf)

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    Error message run time error '62' Input past end of file hit at
    x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(myDir & fn).ReadAll, vbCrLf)
    Did you use the last code I posted?

    If so, upload a txt files that you are using.

  23. #23
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi Jindon,
    The data extraction is working. Just checked. The filesize was 0. That's why it hit the error message. There is 100,000 records to extract.

    1) Need to display the filename eventhough filesize is zeror, sir. To tally the number of files
    2) Column B is not required. Just letting you know the results of the data.
    3) I need to use this file as a template with Macro and Command button. After extracting User will rename to another filename.
    4) Start the extraction of records from row 3 onwards. User will hardcode the Title and name the Cert# at Row1 and Row2

    Regards
    Chitra

  24. #24
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,672

    Re: Search & return all rows contains that word from all txt files with filename

    1) It is already there.
    2) fixed.
    3) Add a Button and assign this macro for yourself
    4) the code now will create a new workbook that you can work and save.
    Please Login or Register  to view this content.

  25. #25
    Forum Contributor
    Join Date
    05-22-2015
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    207

    Re: Search & return all rows contains that word from all txt files with filename

    Hi,
    Thank you for your time and support. It worked like a charm.
    Thank you so much Yaseer and Jindon.

+ 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] Formula Help Pls - Search cell for WORD, return fields from WORD lookup
    By tomski777 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-17-2017, 12:02 AM
  2. how can I return a full filename from partial search
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2014, 06:28 PM
  3. Replies: 2
    Last Post: 08-05-2013, 04:45 PM
  4. Replies: 7
    Last Post: 11-23-2012, 06:06 AM
  5. Search for string in column and return filename
    By patroy33 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-05-2011, 12:05 AM
  6. Replies: 1
    Last Post: 01-25-2011, 10:50 PM
  7. [SOLVED] need formula to search column for a word and return another word
    By Skyline in forum Excel General
    Replies: 5
    Last Post: 11-18-2005, 06:10 PM

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