我有一些代码来搜索csv中的值。
我想搜索一个值,然后当我找到确切的值时, 我将更新该字段以表明我已将此作为参考,请参阅以下内容:
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)
'Check Serial from List
If the_array(R, 3) = strCode Then
readCsv = the_array(R, 3) 'return serial from list
strTestcnt = the_array(R, 1) 'retry count
strProcess = the_array(R, 2) 'process/station
'--->>> in this part I'd like to add a new value on the_array(R, 4) with the value "1"
'--->>> and save it on the csv file, how can i do this?
'--->>> I open the csv file using "fnum = FreeFile"
"Open file_name For Input As fnum"
Exit Function
End If
Next C
End If
Next R
也许你可以帮助我,谢谢
答案 0 :(得分:1)
使用包含数组数组的Variant可以更容易地处理这类事情。
Private Sub CsvUpdate(ByVal Code As String)
Dim F As Integer
Dim RowData As String
Dim RowCols() As String
Dim NewCol As Long
Dim CsvRows As Variant
Dim R As Long
F = FreeFile(0)
Open "old.csv" For Input As #F
ReDim CsvRows(0)
Do Until EOF(F)
Line Input #F, RowData
RowCols = Split(RowData, ",")
If NewCol = 0 Then NewCol = UBound(RowCols) + 1
ReDim Preserve RowCols(NewCol) 'Add new column.
ReDim Preserve CsvRows(R) 'Add new row.
CsvRows(R) = RowCols 'Assign columns to row.
R = R + 1
Loop
Close #F
For R = 0 To UBound(CsvRows)
If Trim$(CsvRows(R)(3)) = Code Then
'Do something....
CsvRows(R)(NewCol) = "1" 'Mark it "processed"
Else
CsvRows(R)(NewCol) = "0" 'Mark it "not processed"
'or just leave null.
End If
Next
On Error Resume Next
Kill "new.csv"
On Error GoTo 0
F = FreeFile(0)
Open "new.csv" For Output As #F
For R = 0 To UBound(CsvRows)
Print #F, Join$(CsvRows(R), ",")
Next
Close #F
End Sub