+ Reply to Thread
Results 1 to 4 of 4

importing data from text file

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    9

    importing data from text file

    Dear experts,

    I need to import a similar text file as given in the sample here, with the following specifications:
    1.in the line #BEGIN abc 1, the no.( 1 in this case)should be in the column 1 of row 1. every time the term #BEGIN appears, data should be imported to a new row.
    2.for all other data below this line, the data below the line beginning with # should be taken into the row/cell ( i.e., blah blah blah in #Name:
    blah blah blah).
    Here's how I need it sample.xlsx.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: importing data from text file

    Sub a()
    Call ReadTextFile
    LR = Cells(Rows.Count, "J").End(xlUp).Row
    n = 1
    For r = 1 To LR Step 16
      Range("A" & n) = n
      Range("B" & n) = Range("j" & r + 3)
      Range("C" & n) = Range("j" & r + 6)
      Range("D" & n) = Range("j" & r + 9)
      Range("E" & n) = Range("j" & r + 12)
      n = n + 1
    Next
    Columns("j").ClearContents
    End Sub
    Sub ReadTextFile()
    folderPath = "C:\Users\user\Desktop\" ' <<<< to be changed
    Filename = "sample.txt"
    sn = CreateObject("scripting.filesystemobject").opentextfile(folderPath & Filename).readall
    sp = Split(sn, vbCrLf)
    n = UBound(sp)
    For j = 0 To n
      Range("J" & j + 1).Value = sp(j)
    Next
     
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: importing data from text file

    Here's my version. Not nearly as compact as patel45.
    Sub ImportData()
    Dim WS As Worksheet
    Dim aCol As Long
    Dim NextRow As Long
    Dim FileToOpen As String
    Dim TextLine As String
    Dim Temp As Variant
    
    FileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    Set WS = ActiveSheet
    With WS
    NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    
    If FileToOpen <> "" Then
        Open FileToOpen For Input As #1
        
        Do
        Line Input #1, TextLine
        If InStr(TextLine, "#BEGIN") > 0 Then
            aCol = 1
            Temp = Split(TextLine)
            WS.Cells(NextRow, aCol) = Temp(UBound(Temp))
            
            Do
                Line Input #1, TextLine
                If TextLine Like ("[#]*:") Then
                    Line Input #1, TextLine
                    aCol = aCol + 1
                    WS.Cells(NextRow, aCol) = TextLine
                End If
            Loop Until EOF(1) Or InStr(TextLine, "#END") > 0
        End If
        NextRow = NextRow + 1
        Loop Until EOF(1)
        Close #1
    End If
    End With
    
    End Sub
    David
    (*) Reputation points appreciated.

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: importing data from text file

    not compact but more flexible.

+ 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. Importing a data from a text file - user-defined file
    By DaveSev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2013, 07:02 PM
  2. Data importing from Text file to Excel
    By yogeshkk2 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2012, 04:38 AM
  3. Importing specific data from a Text file
    By cspellman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2007, 03:42 PM
  4. Importing data from a text file
    By jbaranski in forum Excel General
    Replies: 3
    Last Post: 08-01-2006, 10:10 AM
  5. Importing data from text file
    By tushar_johri in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 05:19 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