+ Reply to Thread
Results 1 to 4 of 4

Parse Text file

Hybrid View

noodle48 Parse Text file 07-22-2011, 11:40 AM
Paul Re: Parse Text file 07-22-2011, 11:49 AM
Leith Ross Re: Parse Text file 07-22-2011, 11:58 AM
noodle48 Re: Parse Text file 07-22-2011, 12:14 PM
  1. #1
    Registered User
    Join Date
    06-23-2011
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Parse Text file

    I have coded this so far:

    Sub FileName()
    ' Open file and place content into worksheet.
    
        Dim dlgOpen As FileDialog
        Dim fields As String, FileName As String, target As String
        Dim fileHandle As Integer
        Dim i As Long, j As Long
    
    
        Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
        With dlgOpen
            'we want just the one file
            .AllowMultiSelect = False
            .Show
            For i = 1 To .SelectedItems.Count
                FileName = .SelectedItems(i)
            Next
        End With
        
        'get counter ready
        j = 0
        
        'prepare the file handle
        fileHandle = FreeFile()
    
        Open FileName For Input As fileHandle
        Do Until EOF(fileHandle)
            Line Input #fileHandle, fields
    
            Range("A1").Offset(j, 0).Value = fields
            j = j + 1
               
        Loop
    
        Close #1
    
    End Sub
    Once the file is inputted into the excel sheet it all goes into column A. I want to use vba to separate each column the end results should have column A and B filled. Attach is the file I used to open and wanting to parse.
    Attached Files Attached Files
    Last edited by noodle48; 07-22-2011 at 12:06 PM. Reason: Solved

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Parse Text file

    Hi Noodle,

    Try adding this bit of code to the end of your macro (just before End Sub):
        Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Space:=True, _
            FieldInfo:=Array(Array(1, 1), Array(2, 1))
    Hope that helps!

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Parse Text file

    Hello noodle48,

    I made a few changes to your code. The fields variable is split using the space delimiter into an 1-D array. This array can then be directly loaded into the cells Ax:Bx in your loop. I have marked the additions in bold text,
    Sub FileName()
    ' Open file and place content into worksheet.
    
        Dim dlgOpen As FileDialog
        Dim fields As String, FileName As String, target As String
        Dim fileHandle As Integer
        Dim i As Long, j As Long
    
    
        Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
        With dlgOpen
            'we want just the one file
            .AllowMultiSelect = False
            .Show
            For i = 1 To .SelectedItems.Count
                FileName = .SelectedItems(i)
            Next
        End With
        
        'get counter ready
        j = 0
        
        'prepare the file handle
        fileHandle = FreeFile()
    
        Open FileName For Input As fileHandle
        Do Until EOF(fileHandle)
            Line Input #fileHandle, fields
    
            Range("A1:B1").Offset(j, 0).Value = Split(fields, " ")
            j = j + 1
               
        Loop
    
        Close #1
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    06-23-2011
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Parse Text file

    Thanks for your inputs but I got the code to work thanks to Paul. That code work great.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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