Hi all... I am trying to search a comma delimited text file for a value. I am using this code to try and achieve the goal, but I am not certain if this is even the right way. I am very new to dealing with arrays. The file I am searching contains around 200 lines which is formatted like this:
I need to find "orange" which is always located after the 3rd comma. Once I used this code I found, it seems to find the first value in the text file and then if I change what the messagebox returns, I get errors. Hopefully someone can help me better understand this. Thanks for any help
Code:
apple, banana, pear, orange, grapes, watermelon
I need to find "orange" which is always located after the 3rd comma. Once I used this code I found, it seems to find the first value in the text file and then if I change what the messagebox returns, I get errors. Hopefully someone can help me better understand this. Thanks for any help
Code:
Sub findit
Dim arr
arr = FileToArray(fpath)
MsgBox arr(1, 1) 'print some values
End Sub
Function FileToArray(fpath) As Variant
Dim txt As String, arr, d, r, c, rv(), u
'read in the entire file
With CreateObject("scripting.filesystemobject").opentextfile(fpath)
txt = .readall()
.Close
End With
arr = Split(txt, ",") 'split lines to an array
u = UBound(Split(arr(0), "|")) 'assume all lines have same # of fields
ReDim rv(1 To UBound(arr) + 1, 1 To u + 1) 'size the output array
'fill the output array
For r = 0 To UBound(arr)
d = Split(arr(r), "|")
For c = 0 To u
rv(r + 1, c + 1) = d(c)
Next c
Next r
FileToArray = rv
End Function