Importing Txt file into Excel
I think the problemn is with the end-of-line character(s). A PC requires a
Carriage Return (Ascii 13) followed by a Line Feed (Ascii 10). Your file was
create on another system that didn't produce both the CR and LF. The code
below will fix any problem with the file. The code will read your file and
create a 2nd file with the corrected end of line (EOL) characters. After
running this code read the new file into excel and see if it solves your
problem.
Sub FixEOL()
Const ForReading = 1, ForWriting = -2, _
ForAppending = 3
CR = Chr(13)
LF = Chr(10)
ReadFile = Application _
.GetOpenFilename(FileFilter:="Text Files (*.txt), *.txt", _
Title:="Select Read File")
If ReadFile = False Then
MsgBox ("No file Selected - Exiting Macro")
End If
WriteFile = Application _
.GetSaveAsFilename(FileFilter:="Text Files (*.txt), *.txt", _
Title:="Select Write File")
If WriteFile = False Then
MsgBox ("No file Selected - Exiting Macro")
End If
Set fs = CreateObject("Scripting.FileSystemObject")
Set fin = fs.OpenTextFile(ReadFile, _
ForReading, TristateFalse)
Set fout = fs.CreateTextFile _
(Filename:=WriteFile, overwrite:=True)
FoundCR = False
Do While fin.AtEndOfStream < True
ReadData = fin.read(1)
Select Case ReadData
Case CR:
If FoundCR = True Then
'two CR in a row write LF inbeteen the two CR
fout.write LF
fout.write CR
Else
FoundCR = True
fout.write CR
End If
Case LF:
If FoundCR = True Then
''Normal sequence CR foloowed by LF
fout.write LF
FoundCR = False
Else
'Bad Sequence LF without CR, Write CR
fout.write CR
fout.write LF
End If
Case Else
If FoundCR = True Then
'Bad Sequence CR without LF, wite LF
fout.write LF
fout.write ReadData
FoundCR = False
Else
'Normal dequence of two character in middle of line
fout.write ReadData
End If
End Select
Loop
fin.Close
fout.Close
End Sub
"Sadie" wrote:
When I import a .txt file into excel all of the data is displayed in one row.
How do I get excel to put the data into the proper rows?
I just open the .txt file and the import wizard opens. The data is delimited
by commas. Sorting the data into columns seems to work fine. Unfortunately,
the data just displays in the first row in different columns. It does not
seem to recognize when the new row should start.
The column that contains the data where the new row should start has the
data from the last column of the previous row in it as well, example:
$1,337.52"01-Jan-2008
The $1,337.52 should be the last cell in the row and the 01-Jan-2008 should
be the first cell of the next row. Instead they are in the same cell.
Therefore, the entire set of data is all in one row!
I am not very experienced and would appreciate any help.
Thanks,
|