Consulting

Results 1 to 3 of 3

Thread: Assistance with bringing specific items from text file into Excel

  1. #1
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    2
    Location

    Assistance with bringing specific items from text file into Excel

    Hello all,

    I'm a complete rookie at VBA and macros so I apologize if this is rudimentary stuff. I searched Google and then the forums here and couldn't find what I was looking for.

    I am trying to extract specific information from a text file and place it in three Excel columns horizontally so that I can then import it into Access as a table. Via Google I was able to get the three specific fields into excel the way I want it, but it only finds the first instance of each field then stops. I want it to scour the entire file and fill the cells with the data.

    The fields I'm looking for in the file will have additional data to the right of the field name, and that data can be different throughout the file.

    I've attached a sample of the raw text and the spreadsheet. The text files can get upwards of 2meg so they contain a lot of data.

    If you could give me a few pointers or send me down the correct path towards where I can learn to do what I need to do I'd greatly appreciate it.

    Cheers,

    AJB
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Assumes that all 3 field types are in each block of data

    As a thought, I added an optional format you can consider


    Private Sub CommandButton1_Click()
        Dim myFile As String, text As String, textline As String, FileNum As Long
        
        Dim outRow As Long
        
        outRow = 1
        
        myFile = Application.GetOpenFilename()
        
        FileNum = FreeFile
        Open myFile For Input As #FileNum
        
        While Not EOF(FileNum)
            Line Input #FileNum, textline
                
            If Left(textline, 4) = "LEN:" Then
                ActiveSheet.Cells(outRow, 1).Value = Trim(textline)
            ElseIf Left(textline, 8) = "SIP_URI:" Then
                ActiveSheet.Cells(outRow, 2).Value = Trim(textline)
            ElseIf Left(textline, 14) = "SIP_SUBDOMAIN:" Then
                ActiveSheet.Cells(outRow, 3).Value = Trim(textline)
                outRow = outRow + 1
            End If
        
        Wend
        
        Close #FileNum
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    2
    Location
    Oh my gosh it was that simple? You really are a wizard. This is perfect thank you very very much! I'm going to take a deeper dive into what you did here and hopefully I can apply it to more fields for another project I'm working on.

    Amazing, thank you!!

Posting Permissions

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