View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
[email protected] djenzovoort@gmail.com is offline
external usenet poster
 
Posts: 16
Default Copy values (loop) from TXT file to excel

JOEL Thanks so much!!! This is fantastic. It's 100% what i was looking
for! Can't i send you a present :)?

Thanks!!!

J.Lemmens



Joel wrote:
I didn't notice the comment at the bottom your posting that you added the
"____" to the file. I thought it was part of the data. This should work


Sub Gettext()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0


Set fsread = CreateObject("Scripting.FileSystemObject")
Set fswrite = CreateObject("Scripting.FileSystemObject")

ReadFileName = "longtext.txt"
WriteFileName = "longtext.csv"


'open files
ReadPathName = MyPath + ReadFileName
Set fread = fsread.GetFile(ReadPathName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)


Do While tsread.atendofstream = False

InputLine = tsread.ReadLine


If (InStr(InputLine, "TEXT") 0) Then

If Len(OutPutLine) 0 Then
tswrite.WriteLine OutPutLine
OutPutLine = ""

End If
Else
If Len(OutPutLine) 0 Then
OutPutLine = OutPutLine + "," + InputLine
Else
OutPutLine = InputLine
End If
End If

Loop

tswrite.Close
tsread.Close

Exit Sub
End Sub


" wrote:

Thanks so much for the helping hand;

This is what the txt file looks like:

_______________________________SHEET 7K294
0,2949.41
276.52,3104.61
TEXT
_______________________________7K294
1.8
NORMAL
271.02,32.11
TEXT
_______________________________TRANSPORTSYSTEM - A
1.5
NORMAL
193.62,32.91
TEXT
_______________________________CENTER SYSTEM
1.5
NORMAL
193.42,28.61
TEXT
_______________________________SFC STEP 4 - WAIT
1.5
NORMAL
193.42,24
TEXT
1256345
1.2
NORMAL
223.55,20
TEXT
A
1.2
NORMAL
195.78,15
TEXT
12-12-06
1.2
NORMAL
193.22,12.1
TEXT
SHEET 7K2946
etc

----------------------------------------

To understand the "important" lines to collect I have put
_______________________________ in from of the data to make it more
understanable in this post.

As you can see i need line 4, 9,14,19 to copy to columns B,C,D,E in
excel (see prev. post).

Thanks so much!!!

Best Regards