+ Reply to Thread
Results 1 to 9 of 9

loop through text files using VBA and extract information

  1. #1
    Registered User
    Join Date
    02-04-2020
    Location
    Ohio
    MS-Off Ver
    365
    Posts
    5

    loop through text files using VBA and extract information

    I work for a sheet metal manufacturer and would like to use VBA to loop through a directory of text files (that are created as setup sheets when machine programs are generated) and extract meaningful information to Excel for further analysis. All of the text files are generally structured the same and below is a sample. I would like to:
    1) Extract the Part Number, Prog Number, Blank Size, Material, Parts/Sheet, and Total Time (under Time Calculations) and place in a table on one sheet.
    2) Extract all the tooling information into a table on a separate sheet. There would be 3 columns: a) prog number (which would repeat for each row of data within the same text file but would link each tool to the program), b) the tool # (e.g. T311), and c) the description (e.g. Sqr 0.531)
    setup sheet template.JPG

  2. #2
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: loop through text files using VBA and extract information

    Should be doable

    Can you attach a proper example of the Text file, and any variations, I noticed you used the term "generally structured" - why? - What differences occour?

    Also if you could attach a mock up of the related output you'd like to see in Excel that would be ideal.

    (Follow the instructions on the Yellow Banner above to see how to attach things )

  3. #3
    Registered User
    Join Date
    02-04-2020
    Location
    Ohio
    MS-Off Ver
    365
    Posts
    5

    Re: loop through text files using VBA and extract information

    I've attached a representative sample of all the txt files. Note that each one of these file sets are stored in a separate directory by machine # so if the code requires slight modification for each type, that would be ok as I could manually copy and paste the 5 outputs together since this is manageable. The file extensions are ".stp" but they can be opened in notepad; I changed the extensions to txt for the purposes of being able to attach them but I'm not sure if this will affect the programming.

    Really the only structural difference is that the file named SETUP SHEETS112349-02-10G-1-1 does not have any tooling information (as it is a laser), so only the first set of information (Part Number, Prog Number, Blank Size, Material, Parts/Sheet, and Total Time) would require extraction. I also realize that some of these fields are blank in the example file I attached so it is ok if there is a blank space when nothing is specified.

    Ideally, it would also convert the total time to a consistent base (e.g. minutes or seconds) but I believe I could write a string formula within Excel to do this separately.

    It will not let me attach the excel output at this time so I will have to post a separate reply to post example excel file that corresponds to the txt file EN02_0_2.

  4. #4
    Registered User
    Join Date
    02-04-2020
    Location
    Ohio
    MS-Off Ver
    365
    Posts
    5

    Re: loop through text files using VBA and extract information

    Here is the excel file, notice how there are two tabs/tables of data; one for the program information and one for the tooling information. This output corresponds to the previously attached file EN02_0_2.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this ‼


    As a starter, you must first open your workbook attachment ('cause of already existing headers),
    paste the below VBA demonstration in a module
    then save this workbook as binary .xlsb in the same folder where are the .stp text files to import
    (or you can easily amend the Path variable within the code) :

    PHP Code: 
    Sub Demo1()
       Const 
    "STAT/TOOL DESCRIPTION "
         
    Dim F%, P$, HRN$, L(), VC&, WT$(), XY&
             
    FreeFile
             P 
    ThisWorkbook.Path Application.PathSeparator
        With Sheet1
    .UsedRange
             H 
    = .Parent.Evaluate(.Rows(1).Address "&""*:*""")
            .
    Offset(1).Clear
        End With
              Sheet2
    .UsedRange.Offset(1).Clear
              Application
    .ScreenUpdating False
              R 
    = [{1,2}]
              
    Dir("*.stp")
        While 
    ""
              
    ReDim L(1 To UBound(H))
              
    Open P For Input As #F
              
    Split(Input(LOF(F), #F), vbCrLf)
              
    Close #F
            
    For 1 To UBound(H)
                
    Application.Match(H(C), V0)
                If 
    IsError(WThen W Application.Match(Replace(H(C), " ""*"), V0)
                If 
    IsNumeric(WThen L(C) = Split(V(1), ": ")(1)
            
    Next
            
    If Application.CountA(LThen
                    W 
    = Empty
                    
    Split(L(6))
                For 
    1 To UBound(XStep 2
                    
    If X(CLike "minute*" Then W Val(X(1)) / 1440 Else _
                    
    If X(CLike "second*" Then W Val(X(1)) / 86400
                Next
                    L
    (6) = W
                    R
    (1) = R(1) + 1
                    Sheet1
    .Cells(R(1), 1).Resize(, UBound(L)).Value L
            End 
    If
                
    Application.Match("*" "*"V0)
            If 
    IsNumeric(WThen
                    ReDim T
    (1 To UBound(V) - 12)
                    
    InStr(V(1), S)
                    
    0
                
    For W To UBound(V)
                       
    Split(RTrim(Mid(V(W), C29)), "/")
                    If 
    UBound(X) = 1 Then
                       Y 
    1
                       T
    (Y0) = L(2)
                       
    T(Y1) = RTrim(X(0))
                       
    T(Y2) = X(1)
                    
    End If
                
    Next
                
    If Y Then
                    Sheet2
    .Cells(R(2), 1).Resize(Y3).Value2 T
                    R
    (2) = R(2) + Y
                End 
    If
            
    End If
              
    Dir
        Wend
        With Sheet1
    .UsedRange.Rows
            
    .Item("2:" & .Count).Columns(6).NumberFormat "[m]:ss_W"
            
    .Columns("A:D").AutoFit
        End With
             Sheet2
    .UsedRange.Range("A:A,C:C").Columns.AutoFit
             Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Registered User
    Join Date
    02-04-2020
    Location
    Ohio
    MS-Off Ver
    365
    Posts
    5

    Re: Hi ! Try this ‼

    Worked great, thank you!

  7. #7
    Registered User
    Join Date
    02-04-2020
    Location
    Ohio
    MS-Off Ver
    365
    Posts
    5

    Re: loop through text files using VBA and extract information

    One more (hopefully simple) request; three of the five file structures have a "material thickness" attribute I would also like to include as a column in the table on the first sheet (this could just be left blank for the other files that don't have this attribute). See the file above EN0114-04-T5_B_1.txt for an example.

    So the columns in the table on the first sheet would now be: Part Number, Prog Number, Blank Size, Material, Material Thickness, Parts/Sheet, and Total Time. Is there an updated version of the code you could send that will add in Material thickness as a column? I've included an updated export file example of what the structure of the table on the first sheet will look like.
    Attached Files Attached Files
    Last edited by greenkevin86; 02-07-2020 at 09:13 AM.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    According to the initial attachment no need any mod if you add the new header directly in column G,
    'cause of the time calculation in column F …

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Or just swap headers between columns E & G …

+ 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. Macro to extract information from files in a specific folder and add to database file
    By luajambeiro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2019, 06:35 PM
  2. [SOLVED] Extract information from the Middle of a bunch of text in a cell
    By KrysBTrying in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 01-23-2019, 05:43 PM
  3. Loop Code to extract information from all files in a folder to a mastersheet
    By flaire14 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2017, 08:34 PM
  4. Extract Information from *.MSG files
    By aravindhan_31 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2015, 03:28 AM
  5. Replies: 0
    Last Post: 10-15-2015, 09:53 AM
  6. Extract information from text
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 12-15-2014, 07:14 AM
  7. Macro to loop through data validation saving extract results as text files
    By Jugo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2013, 10:53 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