Private Sub Form_Load()
Dim blnStartReading1 As Boolean, blnStartReading2 As Boolean
Dim strTestData As String
Dim A As Integer
Dim B As Integer
Dim i As Integer
Dim ii As Integer
Dim strLatitude As String
Dim strLongitude As String
Dim strDepth As String
ChDir App.Path
'Open strfilename For Input As #2
Open "test.txt" For Input As #2
blnStartReading1 = False: blnStartReading2 = False
While Not EOF(2)
Line Input #2, strTestData
If strTestData = "STOP" Then blnStartReading2 = False
If blnStartReading1 = True And blnStartReading2 = True Then
A = Len(strTestData)
For B = 1 To A
If Mid(strTestData, B, 1) = " " Then
strLongitude = Mid(strTestData, 1, B - 1)
strLatitude = Mid(strTestData, B + 1, A)
Exit For
End If
Next B
'MsgBox " " & strLatitude & " " & strLongitude & " " & strDepth & ""
Open App.Path & "/Data.txt" For Append As #3
Print #3, " " & strLatitude & " " & strLongitude & " " & strDepth & ""
Close #3
End If
If blnStartReading1 = True And blnStartReading2 = False Then
i = InStr(strTestData, "174=")
ii = InStr(i + 1, strTestData, ";")
If i < ii And i <> 0 Then
strDepth = Mid(strTestData, i, ii - i)
End If
blnStartReading2 = True
End If
If strTestData = "EndHeader" Then blnStartReading1 = True
If strTestData = "STOP" Then blnStartReading2 = False
Wend
Close #2
End Sub
the code above use to retrieve data from file "test.txt" and then rewrite it to file "data.txt" in another kind of format like below.
3.85663000 100.81888000 174=0
3.85625000 100.81778000 174=0
3.85519000 100.80883000 174=0
3.20013000 101.29401000 174=20
3.20232000 101.29359000 174=20
3.20748000 101.29134000 174=20
i want stop to rewrite the data when the line context in file "test.txt" is "Start 302"
Option Explicit
Private Sub Form_Load()
Dim intFF As Integer
Dim strAll() As String
Dim strItm() As String
Dim strSwp() As String
Dim lngIdxAll As Long
Dim lngIdxItm As Long
intFF = FreeFile
Open "C:\test.txt" For Input As #intFF
strAll = Split(Input(LOF(intFF), #intFF) & vbCrLf, "START ")
'vbCrLf appended so last STOP also has vbCrLf like other STOP lines
Close #intFF
strAll(0) = vbNullString 'remove header
For lngIdxAll = 1 To UBound(strAll)
If Val(strAll(lngIdxAll)) = 302 Then
ReDim Preserve strAll(lngIdxAll - 1) 'remove lines starting at START 302
Exit For
End If
strItm = Split(strAll(lngIdxAll), vbCrLf)
lngIdxItm = InStrRev(strItm(0), " ")
strItm(0) = Mid$(strItm(0), lngIdxItm, InStr(lngIdxItm, strItm(0), ";") - lngIdxItm)
For lngIdxItm = 1 To UBound(strItm) - 2 'exclude first line, STOP line and trailing ""
strSwp = Split(strItm(lngIdxItm), " ")
strItm(lngIdxItm) = strSwp(1) & " " & strSwp(0) & strItm(0) & vbCrLf
'append vbCrLf cause Join() delimiter will be vbNullString
Next
strItm(0) = vbNullString 'remove first line
strItm(UBound(strItm) - 1) = vbNullString 'remove STOP line
strAll(lngIdxAll) = Join(strItm, vbNullString)
Next
Debug.Print Join(strAll, vbNullString)
intFF = FreeFile
Open "C:\testout.txt" For Output As #intFF
Print #intFF, Join(strAll, vbNullString)
Close #intFF
End Sub
for the file "text.txt", i also need to get value in line2 to textbox1, value in line3 to textbox2,value in line4 in textbox3,value in line5 in textbox4. How to write the code by added to the code provide by leinad31 in #2.
Update this line (the one before the outer for loop)
Code:
strAll(0) = vbNullString 'remove header
with
Code:
strItm = Split(strAll(0), vbCrLf) 'transfer each line of header into an array element
Text1.Text = strItm(1)
Text2.Text = strItm(2)
Text3.Text = strItm(3)
Text4.Text = strItm(4)