recognize first three characters in a line?
dear all,
i have a sticky problem. i have some files where, among others, an unknown number of lines starts with "Jxyz" followed with a line starting with "end"; the Jxyz lines are all followed and contain text numbers and commas, randoomly ordered.
I need to perform a certain action on the Jxyz lines and stop the "cycle" when the "end" line is reached.
There are other lines before and after this block of lines, so EOF if not an option.
My problem is: how to "recognize" the "end" line so that I can stop the cycle and go on with the program?
Any help would be marvellous.
Cheers,
GJ
Re: recognize first three characters in a line?
So is this a text file, a database, etc... you need to be a little more specific there.
Also how are you reading this file??
You can always use
Code:
if left$(howeveryourdoingthis, 4) = "Jxyz" then
Do what you need to
end if
Re: recognize first three characters in a line?
I would loop through the code checking for the "end" string, because this is obviouly a defined sign
Code:
' open your file here
Do
'here you need to read in the line line by line into 'sLineString'
LineInput #1, sLineString
if left(sLineString,3) = "end" then exit do
loop
If you tell us what sort of file ( text, doc, xls,database ? )are u using we maybe can help you more. Hope that helps
Re: recognize first three characters in a line?
Or, read it all into a buffer, and work from there. It would be faster.
Code:
Option Explicit
Private Sub Form_Load()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
For x = 0 To UBound(str)
' check for "END" and if so, then Exit For to get out of the loop
st = st & str(x) & vbCrLf & vbCrLf
Next x
MsgBox st
End Sub
There are two ways to count number of lines, as you can see.
Re: recognize first three characters in a line?
Actually, I took the idea of JonnyPoet and worked on it and it worked just the way I wanted.
Thanks u all,
GJ
Re: recognize first three characters in a line?
Quote:
Originally Posted by gjustino
Actually, I took the idea of JonnyPoet and worked on it and it worked just the way I wanted.
Thanks u all,
GJ
You are welcome. Please dont forget ( if not done in between )to sign this post as resolved ( Use the Threadtools in the upper line, so others didn't spend any more attention in this request :)
If you need further help always feel free to ask.