Hi all,
I have a .csv file and I need to evaluate certain fields inside of it. I thought the easiest way would be to convert it to an array and evaluate the elements inside the array (This has been done successfully)...
The array has 8 columns and 100 rows. I want to evaluate the 2nd and 4th column in each row. If the value in the 2nd or 4th column is 1, display a msgbox (or something to indicate a 1 has been detected). This is all done with a button click in a form.
I have a .csv file and I need to evaluate certain fields inside of it. I thought the easiest way would be to convert it to an array and evaluate the elements inside the array (This has been done successfully)...
The array has 8 columns and 100 rows. I want to evaluate the 2nd and 4th column in each row. If the value in the 2nd or 4th column is 1, display a msgbox (or something to indicate a 1 has been detected). This is all done with a button click in a form.
Code:
Private Sub Event_Click()
Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long
file_name = App.Path
If Right$(file_name, 1) <> "\" Then file_name = file_name & "\"
file_name = file_name & "file.csv"
' Load the file.
fnum = FreeFile
Open file_name For Input As fnum
whole_file = Input$(LOF(fnum), #fnum)
Close fnum
' Break the file into lines.
lines = Split(whole_file, vbCrLf)
' Dimension the array.
num_rows = UBound(lines)
one_line = Split(lines(0), ",")
num_cols = UBound(one_line)
ReDim the_array(num_rows, num_cols)
' Copy the data into the array.
For R = 0 To num_rows
If Len(lines(R)) > 0 Then
one_line = Split(lines(R), ",")
For C = 0 To num_cols
the_array(R, C) = one_line(C)
Next C
End If
Next R
End Sub