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.
Bookmarks