+ Reply to Thread
Results 1 to 9 of 9

Extract specific data from .txt file and put it into worksheet excel

  1. #1
    Registered User
    Join Date
    06-28-2013
    Location
    Ghent
    MS-Off Ver
    Excel 2010
    Posts
    4

    Extract specific data from .txt file and put it into worksheet excel

    Hello people,

    i am working in an chemical lab and for the moment i am struggling with following problem. On daily base, a lab instrument generates a txt file containing data. We print those data and put it afterwards manually into an existing excel file. Automatic importing of the txt file is not possible because we only enter the data that is necessary.

    So in attachment you see a txt file with 5 lines containing data. Below you can see the first line:

    u1;Fe ; 112.04;Pb ; 0;Cu ; 5.55;Si ; 12.71;Al ; 16.62;Sn ; 0;Cr ; 4.63;Ag ; 0;B ; 65.44;Zn ; 480.48;Mg ; 8.21;Ca ; 1091.28;P ; 418.2;Ba ; 0;V ; 0;Na ; 4.43;K ; 4.83;Mo ; 435.47;Sb ; 0;Mn ; 1.07;Ni ; 0;Cl ; 53.58;S ; 1464.64;Ti ; 0

    It is my intention to put the data of some well defined components into excel at fixed places. An example: the first values of the line are "u1". The value noted after Fe, in this case 112.04, must be filled in under the column with header Fe1. The value noted after Zn, in this case 480.48, must be filled in under column with header Zn1. Etc for the other 2 elements.

    The first values of the second line are "u2", which means that the values must be filled in under the columns containing (element)2 (example Fe2) etc.

    Data must be added every day, so it is not the intention to overwrite the old data in excel but start at the first free row below the last data.

    I guess my problem can be fixed with some VBA language or so, but i have tried it with no luck due to lack of experience.

    Anyway, thanks beforehand for your help!
    Attached Files Attached Files

  2. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Extract specific data from .txt file and put it into worksheet excel

    Uses a spare sheet ("sheet3"). Change location of text file to suit

    Code can be tidied but does work on test workbook
    Please Login or Register  to view this content.
    Last edited by nigelog; 01-24-2020 at 10:20 AM.

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

    Cool Hi ! Try this ‼


    According to the attachment a pure VBA starter demonstration still working if you change any header

    you must paste to the destination Sheet2 worksheet module :

    PHP Code: 
    Sub Demo1()
        
    Dim S$, HR&, F%, VWL&, C
            S 
    ThisWorkbook.Path "\data_txt.TXT":  If Dir(S) = "" Then Beep: Exit Sub
            H 
    Me.UsedRange.Rows(1).Value2
            R 
    Cells(Rows.Count1).End(xlUp).Row
            F 
    FreeFile
            Open S 
    For Input As #F
            
    Split(Input(LOF(F), #F), vbCrLf)
            
    Close #F
            
    Application.ScreenUpdating False
        
    For Each W In V
            W 
    Split(Replace(W" ; "";"), ";")
            
    Split(W(0), "u")(1)
            
    1
        
    For 1 To UBound(WStep 2
                C 
    Application.Match(W(L) & SH0)
            If 
    IsNumeric(CThen
                
    If F Then R 10Cells(R1).Value Date
                Cells
    (RC).Value2 W(1)
            
    End If
        
    Next LW
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 01-24-2020 at 10:37 AM. Reason: oups ! Typos …

  4. #4
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    157

    Re: Extract specific data from .txt file and put it into worksheet excel

    Hi Lebbersmurf,

    In my previous job I found myself in a similar situation, needing to extract data from a messy TXT output from a DOS application.
    If the output file has always the same formatting/lenght, and you need extracting the information at fixed locations, all you need to do is record a macro. Clean up the file once and the next time just run the macro.

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

    Re: Extract specific data from .txt file and put it into worksheet excel

    You must change the header;
    1) Remove space between chemical symbol and the number. e.g. Fe 2 should be Fe2 etc.
    2) Ca1, Ca2 , Ca3, Ca3, Ca4 should be Ca1, Ca2 , Ca3, Ca4, Ca5
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-28-2013
    Location
    Ghent
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Extract specific data from .txt file and put it into worksheet excel

    Hi DMA,

    thank you for the feedback. In my case, this is not a solution, the values differ in length so fixed locations are unfortunately not an option.

  7. #7
    Registered User
    Join Date
    06-28-2013
    Location
    Ghent
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Extract specific data from .txt file and put it into worksheet excel

    Hi Marc,

    thanks for your input. I receive an error when running your code : "invalid use of me keyword" Any idea what is getting wrong?

    Thanks a lot

  8. #8
    Registered User
    Join Date
    06-28-2013
    Location
    Ghent
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Extract specific data from .txt file and put it into worksheet excel

    Thanks a lot Jindon, this code is the solution for my problem!!!

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

    Arrow

    Quote Originally Posted by Lebbersmurf View Post
    Any idea what is getting wrong?
    You just misread and did not apply the direction

+ 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. Extract Specific Data from one Worksheet to Another Automatically
    By kschmit1 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-03-2017, 09:55 AM
  2. Replies: 1
    Last Post: 02-22-2013, 12:20 PM
  3. extract specific data from a file
    By darkhorse4321 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-18-2011, 08:40 AM
  4. extract specific data from a file into spreadsheet
    By darkhorse4321 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-10-2011, 04:56 AM
  5. extract specific data from a file into worksheet
    By darkhorse4321 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-09-2010, 02:07 AM
  6. extract specific data from web to excel worksheet using button
    By stoey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2009, 06:31 PM
  7. Extract specific row from specific section in an excel file
    By yodha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2009, 01:27 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