+ Reply to Thread
Results 1 to 14 of 14

Import a text file via macro, then select and import certain data.

  1. #1
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Import a text file via macro, then select and import certain data.

    Hi all,

    Macro noob here, would love some help.

    I have a simple Excel table where I would like to import a .txt file via the button on the spreadsheet and auto populate the cells 'calibration flashed'.

    So UNIT 1 would populate cells C8 and C9 with the info contained in the UNIT 1 section of the .txt file and so on.

    The reason this is difficult is there is a set .txt format, so some bits of info will need to be ignored in the txt file.

    Documents are attached as is a screen shot of the sheet and the text file.

    EXAMPLE.PNG

    This is a simple version of what I'd like in the end, but this will help me with some starter macros.

    Thanks for any help!
    James
    Attached Files Attached Files

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,450

    Re: Import a text file via macro, then select and import certain data.

    The attached may help, Note: i have removed your merged cells and replaced them with: Centre accross selection formatting.

    Code in file below:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    If things don't change they stay the same

  3. #3
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    Thank you very much.

    Could you give me a hint as to how to change that line to enable a 'browse for file' rather than a dedicated path?

    Attachment 781919

  4. #4
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    Thank you very much.

    Could you give me a hint as to how to change that line to enable a 'browse for file' rather than a dedicated path?

  5. #5
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,450

    Re: Import a text file via macro, then select and import certain data.

    Maybe like this:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    Thank you for the aid - I'm trying to make the macro more complex but am struggling to make it heading dependent.

    If you take a look at the screenshot below you will see what I am trying to achieve:

    Attachment 782244

    Please could you take a look at the attached documents and advise on some changes?

    Regards

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

    Cool Hi, try this …


    First, according to forum rules thanks to not create any duplicate thread anymore …

    According to your last attachment as it is (no mod is required and no needs to 'Clear Cells') a VBA basics demonstration for starters :

    PHP Code: 
    Sub Demo1()
      Const 
    ": "
        
    Dim VR&, WX(10)
            
    Application.GetOpenFilename("Text files,*.txt"):  If False Then Exit Sub
            R 
    FreeFile
            Open V 
    For Input As #R
            
    Split(Input(LOF(R), #R), vbCrLf)
            
    Close #R
            
    6
        
    While Not IsEmpty(Cells(R2))
            
    Application.Match("Reading Part Numbers and IDs from: " Cells(R2).TextV0)
            If 
    IsNumeric(WThen X(00) = Split(V(1), D)(1): X(10) = RTrim(Split(V(3), D)(1))
            
    Cells(24).Resize(2) = X
            Erase X
            R 
    5
        Wend
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  8. #8
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    Apologies for the dual post.

    Thanks for the code, very helpful.

    I am trying to make it work in a different way, the text file I have supplied is much simpler than what I will actually use (I have simplified it / removed private info).

    For the Macro to work it needs to:

    1. Search for a specific line ''Reading Part Numbers and IDs from: PDM''
    2. then it needs to pick the Software version and Calibration version from that section
    3. then it will place it in the correct cells (which it already does)

    this then needs to be repeated for each of the 4 sections (PDM, CBM, BBH, BBH2). This will then mean when the text file is much larger it will search for a specific line, then pick the Software version and Calibration version from that section.

    See image:

    Attachment 782319

    That is the last iteration I need, sorry I'm really new to this and struggling through.

    Thank again all - if you could help with this last iteration that would be great and I will continue to learn.

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

    Arrow Re: Import a text file via macro, then select and import certain data.


    As your last attachment is not valid …

    I can't catch what you mean, just try my VBA demonstration with your post #6 attachment …

  10. #10
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    It works very well, but only for that specific text file. I'll add the image as an attachment so you can see it correctly.

    Your code works, but to work with the mode complext .txt file it needs to be edited to work as mentioned above.

    Hopefully the image is attached.
    Attached Images Attached Images

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

    Arrow Re: Import a text file via macro, then select and import certain data.


    According to your post #6 attachment it is exactly what my VBA demonstration does …

    So you must well elaborate your need with details in order there is nothing to guess as guessing can't be coding !
    As proceeding like you did means you are enough confident with your Excel / VBA skills to fit any helper code
    but if it's not the case you just wasted helpers time …

    You can attach a workbook with a before state worksheet and an expected result worksheet
    according to a 'sanitized' exact layout source text file …

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

    Arrow Re: Import a text file via macro, then select and import certain data.


    Quote Originally Posted by jamesrawlinson View Post
    It works very well, but only for that specific text file.
    As it also works with your initial post text file …

  13. #13
    Registered User
    Join Date
    05-27-2022
    Location
    London
    MS-Off Ver
    Version 2010
    Posts
    10

    Re: Import a text file via macro, then select and import certain data.

    Thank you. Certainly not trying to waste time, just trying to learn and make this work.

    As request I have made a full file to see the issue. I have also attached two images to make the request clear.

    The info in the text file that is required is right at the bottom of the text.
    Attached Images Attached Images
    Attached Files Attached Files

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

    Lightbulb Last try …


    Thanks for the rep' !

    Your initial explanation is so far from your real need, to really avoid if you are not able to fit any helper code …
    Accuracy is the only path to an efficient solution.

    According to the last attachment another Excel / VBA basics demonstration where 'Clear Cells' button is useless :

    PHP Code: 
    Sub Demo2()
        
    Dim VR&, WSXYL&
            
    Application.GetOpenFilename("Text files,*.txt"):  If False Then Exit Sub
            R 
    FreeFile
            Open V 
    For Input As #R
            
    Split(Input(LOF(R), #R), "Filter set OK for ECU: ")
            
    Close #R
            
    = [{"--Software Version (JLR) * : *","--Calibration Version (JLR) * : *","--Veh Mfr Software Number * : *"}]
        For 
    6 To Cells(Rows.Count2).End(xlUp).Row
            With Cells
    (R2).CurrentRegion.Rows
               
    .Item("3:" & .Count).Columns(3).ClearContents
                S 
    = .Cells(1) & vbCrLf "*"
            
    For Each X In V
                
    If X Like S Then
                    Y 
    Split(XvbCrLf)
                For 
    3 To .Count
                        X 
    Application.Match(.Cells(L1), [{"SW Version ","Calibration Version ","Mfr SW Number"}], 0)
                    If 
    IsNumeric(XThen
                        X 
    Application.Match(W(X), Y0)
                        If 
    IsNumeric(XThen .Cells(L3) = Split(Y(1), ": ")(1)
                    
    End If
                
    Next
                    
    Exit For
                
    End If
            
    Next
                R 
    + .Count
            End With
        Next
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 06-01-2022 at 06:19 AM. Reason: little tweak …

+ 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 import data from text file into seperate columns
    By tjstreetta in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-11-2018, 07:32 AM
  2. Import data from text, by Macro, With a text box for the file path??
    By locopete999 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2016, 06:30 AM
  3. Macro - Open File Explorer, Select File, Import certain information from file
    By Drayde in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2015, 08:58 AM
  4. Replies: 7
    Last Post: 10-30-2012, 07:14 AM
  5. [SOLVED] Import text file from VBA code; import all numbers as text
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-19-2012, 05:19 PM
  6. File import - select file and import sheet(1) from closed workbook
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2011, 03:15 AM
  7. macro to import text file
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2008, 09:08 AM

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