How would i set a string to a line in a txt file?
Also how can I count how many lines there are in a txt file?
Printable View
How would i set a string to a line in a txt file?
Also how can I count how many lines there are in a txt file?
With two ways to count the lines :)
VB 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) st = st & str(x) & vbCrLf Next x MsgBox st End Sub
str() is split by lines.
Many ways for this.
You can open the file and loop reading each line and incrementing a counter varable.
Open the file all in one shot into an string array. Then split it and get the UBound of the array.
'...
'...
Is this a large file?
It depends, maybe a few hundred lines or more.
How can I set each line into a string array that would be best I think.
My code above reads the whole file in, and then splits it by carriage return-line feed vbCRLF, resulting in an array str() of lines.
Im getting an expected array error.
i is declared as 1.
List1.AddItem st(i)
No. st is a string that is dispayed as a msgbox. It includes the whole file and adds linefeeds to it.
Use str(i). And use Ubound() to get the upper bound.
How would I go about doing that, str(i) gave me subscript out of range runtime error 9, UBound(str) gave me all 2's.
Post your code. Ubound()=2 means that there were only 3 lines read in.
Are you reading in a text file? Does it use vbCRLF for each line?
Here it is.
VB Code:
Private Sub Command2_Click() Dim x As Integer, st As String Dim ff As Integer Dim strBuff As String Dim str() As String ff = FreeFile If Dir$(App.Path & "\" & File1 & ".txt") = "" Then MsgBox "File does not exist please check your spelling" Else Open App.Path & "\" & File1 & ".txt" For Input As #ff strBuff = Input(LOF(ff), ff) Close #ff str() = Split(strBuff, vbCrLf) For x = 0 To UBound(str) st = st & str(x) & vbCrLf Next x MsgBox st Label1.Caption = "Lines: " & UBound(str) + 1 Do Until (i > UBound(str) + 1) List1.AddItem str(i) i = i + 1 Loop End If End Sub
You have an extra ELSE in there.
VB Code:
Private Sub Command2_Click() Dim x As Integer Dim ff As Integer Dim strBuff As String Dim str() As String ff = FreeFile If Dir$(App.Path & "\" & File1 & ".txt") = "" Then MsgBox "File does not exist please check your spelling" Else Open App.Path & "\" & File1 & ".txt" For Input As #ff strBuff = Input(LOF(ff), ff) Close #ff str() = Split(strBuff, vbCrLf) For x = 0 To UBound(str) List1.AddItem str(x) Next x Label1.Caption = "Lines: " & UBound(str) + 1 End If End Sub
This should be all you need. You don't need to build the string for the msgbox or the msgbox.
Thanks, you have helped me a ton.