View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Copy values (loop) from TXT file to excel

Let me know if this works. I think there may be some problems with the
example you gave. change the path name, input file, and output file as
needed. the program creates a csv file which can be opened in excel after
the macro is run.

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

'remove spaces at beginning of line
Do While Left(InputLine, 1) = " "
InputLine = Mid(InputLine, 2)
Loop

If (Left(InputLine, 3) = "___") Then

If Len(OutputLine) 0 Then
OutputLine = OutputLine + ";"
tswrite.WriteLine OutputLine
OutputLine = ""
End If

'remove underline
Do While Left(InputLine, 1) = "_"
InputLine = Mid(InputLine, 2)
Loop
OutputLine = InputLine
Else
OutputLine = OutputLine + "," + InputLine
End If

Loop

If Len(OutputLine) 0 Then
OutputLine = OutputLine + ";"
tswrite.WriteLine OutputLine
OutputLine = ""
End If


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