+ Reply to Thread
Results 1 to 5 of 5

Get Value In Between Strings From All Text Files In Folder

Hybrid View

exc4libur Get Value In Between Strings... 08-26-2016, 05:45 PM
AlphaFrog Re: Get Value In Between... 08-26-2016, 06:11 PM
exc4libur Re: Get Value In Between... 08-26-2016, 06:45 PM
AlphaFrog Re: Get Value In Between... 08-26-2016, 06:49 PM
exc4libur Re: Get Value In Between... 08-26-2016, 07:25 PM
  1. #1
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Get Value In Between Strings From All Text Files In Folder

    Hello!
    How are we today? Fine I hope.

    I am trying to go through all text files inside a folder and extract the value found in between strings to my worksheet.
    The strings to look for are in columns, the beginning string to look for in row 1 and the ending string in row 2.
    Please find attached a sample workbook and text file. Also, I have pasted the code of what I have so far.

    Thanks in advance for taking the time and trying help

    Sub ExtractValueInBetween()
        Dim filename As String, nextrow As Long, MyFolder As String
        Dim MyFile As String, text As String, textline As String, beginStr As String, endStr As String
        
        MyFolder = "C:\test\" 'I need to make this work as a FileDialog Picker!!
        MyFile = Dir(MyFolder & "*.txt")
    
        Do While MyFile <> ""
            Open (MyFolder & MyFile) For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                text = text & textline
            Loop
            Close #1
            MyFile = Dir()
            Debug.Print text
            beginStr = InStr(text, Range("b1").Value)
            endStr = InStr(text, Range("b2").Value)
            nextrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
            ActiveSheet.Cells(nextrow, "b").Value = Mid(text, beginStr, endStr - beginStr)
            text = ""
        Loop
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Get Value In Between Strings From All Text Files In Folder

    Sub ExtractValueInBetween()
        Dim filename As String, nextrow As Long, MyFolder As String, c As Long
        Dim MyFile As String, text As String, textline As String, beginStr As String, endStr As String
        
        MyFolder = "C:\test\" 'I need to make this work as a FileDialog Picker!!
        MyFile = Dir(MyFolder & "avar*.txt")
        
        Do While MyFile <> ""
            Open (MyFolder & MyFile) For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                text = text & textline
            Loop
            Close #1
            MyFile = Dir()
            Debug.Print text
            nextrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
            For c = 2 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
                beginStr = InStr(text, Cells(1, c).Value) + Len(Cells(1, c).Value)
                endStr = InStr(text, Cells(2, c).Value)
                ActiveSheet.Cells(nextrow, c).Value = Mid(text, beginStr, endStr - beginStr)
            Next c
            text = ""
        Loop
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Get Value In Between Strings From All Text Files In Folder

    Genius!!!!!!!!!!!!!!!!!!!!!!!!! YES!!!!!!!!!
    Could you add just one quick thing, the filename on column A please!
    Thank you sooo much!!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Get Value In Between Strings From All Text Files In Folder

    You're welcome.

    Sub ExtractValueInBetween()
        Dim filename As String, nextrow As Long, MyFolder As String, c As Long
        Dim MyFile As String, text As String, textline As String, beginStr As String, endStr As String
        
        MyFolder = "C:\test\" 'I need to make this work as a FileDialog Picker!!
        MyFile = Dir(MyFolder & "*.txt")
        
        Do While MyFile <> ""
            Open (MyFolder & MyFile) For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                text = text & textline
            Loop
            Close #1
                    
            Debug.Print text
            nextrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
            ActiveSheet.Cells(nextrow, 1).Value = MyFile
            For c = 2 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
                beginStr = InStr(text, Cells(1, c).Value) + Len(Cells(1, c).Value)
                endStr = InStr(text, Cells(2, c).Value)
                ActiveSheet.Cells(nextrow, c).Value = Mid(text, beginStr, endStr - beginStr)
            Next c
            MyFile = Dir
            text = ""
        Loop
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Get Value In Between Strings From All Text Files In Folder

    I couldn't thank you enough. Once again, thank you! It was killing me.

+ 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. Replies: 1
    Last Post: 01-28-2019, 06:55 PM
  2. [SOLVED] Search and Replace Unique Text Strings Over Multiple Files
    By portokie in forum Excel General
    Replies: 2
    Last Post: 07-11-2016, 07:24 PM
  3. How to concatenate two strings from two different .dat based text files using VBA
    By mahendra.asapu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2015, 06:25 PM
  4. Read strings from 2 reference text files
    By scottich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2014, 11:57 AM
  5. Copy excel and text files from one folder to another folder...
    By annupojupradeep in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 06:34 AM
  6. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  7. import all text files in folder
    By shoshanako in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2008, 10:01 AM

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