[RESOLVED] how to get data from a specific line?
I want to get data from a file? how to put data in line 2 to a textbox, data in line 3 to a textbox and continue..........
i using code below but come an error "type mismatch" and ReDim Preserve MyArray(i) was highlighted.
Dim temp As String, MyArray As Variant
Open "filename.txt" For Input As #1
While Not EOF(1)
ReDim Preserve MyArray(i)
Line Input #1, MyArray(i)
i = i + 1
Wend
Close
Re: how to get data from a specific line?
Moved from FAQ forum
Welcome to VBForums :wave:
The problem is that you did not declare it as an array, you only declared it as a variant. You should have it like this instead (but preferably using a data type other than Variant):
VB Code:
Dim temp As String, MyArray() As Variant
Re: how to get data from a specific line?
i have change the code as below.But,still got problem.An error "subscript out of range" occurred & {Msgbox "" & MyArray(2)}was highlight.
Dim temp As String, MyArray() As Variant
Open "C:\MY3C0540.daf" For Input As #1
While Not EOF(1)
ReDim Preserve MyArray(i)
Line Input #1, MyArray(i)
i = i + 1
MsgBox "" & MyArray(2)
Wend
Close
Re: how to get data from a specific line?
depending on what you're doing - you'll probably find it easier to load the whole file in at once and then split it:
VB Code:
Dim MyArray() [B]As String[/B]
Open "C:\MY3C0540.daf" For Input As #1
MyArray = Split(Input(LOF(1), #1), vbCrLf)
Close #1
MsgBox MyArray(2) 'the third line in the file (because the array is 0-based)
Re: how to get data from a specific line?
Quote:
Originally Posted by junlo
i have change the code as below.But,still got problem.An error "subscript out of range" occurred & {Msgbox "" & MyArray(2)}was highlight.
Dim temp As String, MyArray() As Variant
Open "C:\MY3C0540.daf" For Input As #1
While Not EOF(1)
ReDim Preserve MyArray(i)
Line Input #1, MyArray(i)
i = i + 1
MsgBox "" & MyArray(2)
Wend
Close
MyArray(2) would be the 3rd line in the file. Are there at least 3 lines?
Re: how to get data from a specific line?
Quote:
Originally Posted by bushmobile
depending on what you're doing - you'll probably find it easier to load the whole file in at once and then split it:
VB Code:
Dim MyArray() [B]As String[/B]
Open "C:\MY3C0540.daf" For Input As #1
MyArray = Split(Input(LOF(1), #1), vbCrLf)
Close #1
MsgBox MyArray(2) 'the third line in the file (because the array is 0-based)
This would be a faster method than loading it line by line. However, if the text file is really large than I would do it line by line.
VB Code:
Option Explicit
Private Function GetLineFromFile(ByVal FilePath As String, ByVal LineNumber As Long) As String
Dim lonFF As Long, lonLine As Long
Dim strLine As String
lonFF = FreeFile
Open FilePath For Input As #lonFF
Do While Not EOF(lonFF)
Line Input #lonFF, strLine
lonLine = lonLine + 1
If lonLine = LineNumber Then
GetLineFromFile = strLine
Exit Do
End If
Loop
Close #lonFF
End Function
Private Sub Form_Load()
Text1.Text = GetLineFromFile("C:\MY3C0540.daf", 2)
End Sub
Re: how to get data from a specific line?
Quote:
Originally Posted by MartinLiss
MyArray(2) would be the 3rd line in the file. Are there at least 3 lines?
Not right after the first line was read, Martin. :) The error will occur until the 3rd line is read and the array has 3 elements. (And this is code with no Option Explicit, too.)
Re: how to get data from a specific line?