LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Line Input Not behaving...!

Here is a program I wrote probably 20 years ago in c language and converted
it to VBA a few years ago. Your file was probably generate in a Unix/Linux
operating system. Yo need to read the file as a single character at a time
instead of a line at a time.


Sub FixEOL()

Const ForReading = 1, ForWriting = -2, _
ForAppending = 3


CR = vbCR
LF = vbLF

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




"Andrew" wrote:

*** Trying to read an External CSV file ***
however Have found the EOL marker in the file is CHR(10) and not
Chr(10)+Chr(13).
The CSV file is from an external Source and i am unable to Change the
Format (EOL character etc.).

Sub Import ( )
CSVLine = 0
CsvFile = Application.GetOpenFilename("Comma Sep Values (*.csv),
*.csv")
Open CsvFile For Input As #1

While Not EOF(1)
Line Input #1, aRecord
' Other Parsing Code Is Here..
' ...
' ...
CSVLine = CSVLine + 1
Wend

Close #1
msgbox Str(CSVLine)+": Lines Read" '*** CSVLine Only ever reaches 1
***

End Sub

Is there another Setting to Change the behaviour of Line Input #1
.... ?
the Lines are not a set length so am unable to read a set number of
Characters etc.

Any Feedback would be Appreciated.
Andrew



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Line Input not finding Chr10 or 13 [email protected] Excel Programming 8 March 17th 07 08:12 PM
Multi line merged cell - adapting to different size input - possible? David Smithz Excel Discussion (Misc queries) 1 June 18th 06 04:21 PM
diagonal line and data input John Excel Discussion (Misc queries) 3 January 25th 06 12:29 AM
line input problem-need to return 5 lines after a string is found festdaddy Excel Programming 3 December 8th 05 04:38 PM
Reads entire *.txt file into string opposed to a desired line by line input. ej_user Excel Programming 3 October 11th 04 07:15 PM


All times are GMT +1. The time now is 09:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"