Consulting

Results 1 to 6 of 6

Thread: Extract string from multiple files using vba

  1. #1
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location

    Extract string from multiple files using vba

    I need to extract a data from a set of text files inside a folder. I tried several times without success, I hope that someone can help me.

    All the files I have to read are inside the folder C:/test. The data I need to extract from text file is located after a key word.

    The data should be placed in an excel file (every data copied from a single text file inside a different cell).

    Is there someone that can help me? I tried (with success) to write the macro for one single file, but I don't know how I can do it for all the files in my folder!!

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    try:

    Sub run_macro_on_all_files()
        
        Dim strPath As String
        Dim strFiles As String
        
        strPath = "C:\test\"
        If Not Dir(strPath & "*.txt") = "" Then 'test for existence of txt files
            strFiles = Dir(strPath & "*.txt")
            Do Until strFiles = ""
                'paste your code here
                '...
                '...
                '...
                strFiles = Dir
            Loop
        End If 
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location
    Hi, and thanks for your help

    you can see my code below. I don't receive any errors, but my excel file is blank..

    Sub run_macro_on_all_files()

    Dim strPath As String
    Dim strFiles As String
    Dim cella

    strPath = "C:\prova\"
    If Not Dir(strPath & "*.txt") = "" Then 'test for existence of txt files
    strFiles = Dir(strPath & "*.txt")
    Do Until strFiles = ""

    Open strFiles For Input As #1

    Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
    Loop
    'Close file
    Close #1
    ReadBRTLuminance = InStr(text, "Read BRT Luminance")
    ActiveCell.Offset(cella, 1).Value = Mid(text, ReadBRTLuminance + 31, 9)
    cella = cella + 1


    strFiles = Dir
    Loop
    End If

    End Sub

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    please use code tags when posting your code to the thread. When you click # button in Quick Reply code tags (without spaces) will be inserted.

    [ CODE ]paste your code between these tags[ /CODE ]

    you can find a number of examples about reading a txt file content here at VBAX.

    below worked for me:

    Sub ReadTextFile_FindString_WriteToCell()
    
        Dim strPath As String, strFiles As String, strTxtFile As String    Dim strToFind As String, strToWrite As String
        Dim cella As Long
        
        strPath = "C:\prova\"
        strToFind = "Read BRT Luminance"
        cella = 0
        
        If Dir(strPath & "*.txt") = "" Then 'test for existence of txt files
            MsgBox "No txt files in the directory: " & strPath
            Exit Sub
        End If
            
        strFiles = Dir(strPath & "*.txt")
        Do Until strFiles = ""
            strTxtFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath & strFiles).ReadAll
            strToWrite = Mid(strTxtFile, InStr(strTxtFile, strToFind) + 31, 9)
            ActiveCell.Offset(cella, 1).Value = strToWrite
            cella = cella + 1
            strFiles = Dir
        Loop
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    3
    Location
    IT WORKS!!!!!!!!!!!!!!!!!!!

    Thanks, you're great!

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    mark the thread from Thread Tools dropdown which is above the first message for future references..
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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