+ Reply to Thread
Results 1 to 2 of 2

Import Text file Fields as Columns in Excel

Hybrid View

  1. #1
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Import Text file Fields as Columns in Excel

    Hi all,

    I have a text file that contains more than 1 lakh lines.
    the text file contains 2 fields, the field length of the 1st field is 22 chars.
    I wanted to import that text file into excel spreadsheet.
    Since the lines in text file is more than 1 lakh, I got help on this forum from RoyUK, to split the text file and write into 2 spreadsheet.
    Code:
    Option Explicit
    
    Sub LargeFileImport()
        Dim ResultStr As String
        Dim FileName As String
        Dim FileNum As Integer
        Dim Counter As Double
        'Ask User for File's Name
        FileName = Application.GetOpenFilename
        'Check for no entry
        If FileName = "" Then End
        'Get Next Available File Handle Number
        FileNum = FreeFile()
        'Open Text File For Input
        Open FileName For Input As #FileNum
        'Turn Screen Updating Off
        Application.ScreenUpdating = False
        'Create A New WorkBook With One Worksheet In It
        Workbooks.Add Template:=xlWorksheet
        'Set The Counter to 1
        Counter = 1
        'Loop Until the End Of File Is Reached
        Do While Seek(FileNum) <= LOF(FileNum)
            'Display Importing Row Number On Status Bar
            Application.StatusBar = "Importing Row " & Counter & " of text file " _
                                    & FileName
            'Store One Line Of Text From File To Variable
            Line Input #FileNum, ResultStr
            'Store Variable Data Into Active Cell
            If Left(ResultStr, 1) = "=" Then
                ActiveCell.Value = "'" & ResultStr
            Else
                ActiveCell.Value = ResultStr
            End If
            If ActiveCell.Row = 65536 Then
                'If On The Last Row Then Add A New Sheet
                ActiveWorkbook.Sheets.Add
            Else
                'If Not The Last Row Then Go One Cell Down
                ActiveCell.Offset(1, 0).Select
            End If
            'Increment the Counter By 1
            Counter = Counter + 1
            'Start Again At Top Of 'Do While' Statement
        Loop
        'Close The Open Text File
        Close
        'Remove Message From Status Bar
        Application.StatusBar = False
    End Sub
    What current code does:
    It reads lines and writes into excel spreadsheet in a active sheet and read line by line.
    What I want:
    I want some changes in the above code that will read the line as 2 fields and write the value of 1st field(22 chars) in column A and the 2nd field in Column B.
    I want the writing/importing to be done from the first cell of the excelsheet and not from any active sheet.

    Hoping for a solution.
    Thanks in advance.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  2. #2
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Import Text file Fields as Columns in Excel

    I have modified my text file with comma delimited file and the following code gives me what I want, but with one issue.
    The comma is also written in the first column.
    I dont want the comma to be written.
    Is there any way to do so.
    Have attached the workbook with the macro and the text file for testing.
    code:
    Sub LargeTextFileImport()
        'In the event of an error, make sure the application is reset to
        'normal.
        'Dimension Variables
        Dim ResultStr As String
        Dim FileName As String
        Dim FileNum As Integer
        Dim Counter As Double
        Dim CommaCount As Integer
        Dim WorkResult As String
        'Ask for the name of the file.
        FileName = InputBox("Please type the name of your text file, for example, test.txt")
        'Turn off ScreenUpdating and Events so that users can't see what is
        'happening and can't affect the code while it is running.
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        'Check for no entry.
        If FileName = "" Then End
        
        'Get next available file handle number.
        FileNum = FreeFile()
        
        'Open text file for input.
        Open FileName For Input As #FileNum
        
        'Turn ScreenUpdating off.
        Application.ScreenUpdating = False
        'Set the counter to 1.
        Counter = 1
        'Place the data in the first row of the column.
        Range("A1").Activate
        'Loop until the end of file is reached.
        Do While Seek(FileNum) <= LOF(FileNum)
            'Show row number being imported on status bar.
            Application.StatusBar = "Importing Row " & _
                    Counter & " of text file " & FileName
            'Store one line of text from file to variable.
            Line Input #FileNum, ResultStr
            'Initialize the CommaCount variable to zero.
            CommaCount = 0
            
            'Store the entire string into a second, temporary string.
            WorkResult = ResultStr
            'Parse through the first line of data and separate out records
            '257 to 510.
            While CommaCount < 255
                WorkResult = Right(WorkResult, Len(WorkResult) - InStr(1, WorkResult, ","))
                CommaCount = CommaCount + 1
            Wend
            'Parse out any leading spaces.
            If Left(WorkResult, 1) = " " Then WorkResult = Right(WorkResult, Len(WorkResult) - 1)
            'Ensure that any records that contain an "=" sign are
            'brought in as text, and set the value of the current
            'cell to the first 256 records.
            If Left(WorkResult, 1) = "=" Then
                ActiveCell.Value = "'" & Left(ResultStr, Len(ResultStr) - Len(WorkResult))
            Else
                ActiveCell.Value = Left(ResultStr, Len(ResultStr) - Len(WorkResult))
            End If
            'Ensure that any records that contain an "=" sign are
            'brought in as text,and set the value of the next cell
            'to the last 256 records.
            If Left(WorkResult, 1) = "=" Then
                ActiveCell.Offset(0, 1).Value = "'" & WorkResult
            Else
                ActiveCell.Offset(0, 1).Value = WorkResult
            End If
            'Move down one cell.
            'ActiveCell.Offset(1, 0).Activate
            If ActiveCell.Row = 65536 Then
                'If On The Last Row Then Add A New Sheet
                'ActiveWorkbook.Sheets.Add
                E_Ensemb1.Activate
                Range("A1").Activate
            Else
                'If Not The Last Row Then Go One Cell Down
                ActiveCell.Offset(1, 0).Select
            End If
            'Increment the Counter by 1.
            Counter = Counter + 1
            'Start again at top of 'Do While' statement.
        Loop
        'Close the open text file.
        Close
     End Sub
    Thanks in advance.
    Attached Files Attached Files

+ 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