Results 1 to 2 of 2

Opening a space delimited text file in Excel

Threaded View

  1. #1
    Registered User
    Join Date
    02-21-2009
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Opening a space delimited text file in Excel

    I wan to open the txt file in excel using the below code...however it only can help me open without separate the comma, space into column in excel...how will be the code?

    Please help!


    Sub Import()
        Open "d:\data.txt" For Input As #1
        R = 1
        While Not EOF(1) 'Scan file line by line
            C = 1
            Entry = ""
            Line Input #1, Buffer
            Length = Len(Buffer)
            i = 1
            While i <= Length 'split comma-delimited string into cells
                If (Mid(Buffer, i, 1)) = "," Then
                    With Application.Cells(R, C)
                        .NumberFormat = "@" 'Text formatting
                        .Value = Entry
                    End With
                    C = C + 1
                    Entry = ""
                Else
                    Entry = Entry + Mid(Buffer, i, 1)
                End If
                i = i + 1
            Wend
            If Len(Entry) > 0 Then
                With Application.Cells(R, C)
                    .NumberFormat = "@" 'Text formatting
                    .Value = Entry
                End With
            End If
            R = R + 1
        Wend
        Close #1
    End Sub
    Last edited by VBA Noob; 04-25-2009 at 02:29 AM.

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