
我總是將資料【AAA,BBB,CCC】寫入CSV檔案。
如果文字檔案中有其他資料(例如:DDD),我還想寫入。
數組:
arr = 陣列("AAA", "BBB", "CCC")
文字檔案(讀取):
AAA
BBB
CCC
DDD
BBB
CCC
AAA
DDD
AAA
代碼:
Open InputFile For Input As #1
Open OutputFile For Output As #2
Do Until EOF(1)
Line Input #1, strData
'Read data from text file
'Check text file data exists or not in arr
'If not exist, add items(DDD) to arr
Print #2, strData
Loop
Close #1
Close #2
CSV 檔案(寫入):
AAA, BBB, CCC, DDD
這樣做的最佳方法是什麼?
答案1
濾波函數 (Visual Basic)
傳回一個從零開始的數組,其中包含基於指定過濾條件的 String 數組的子集。
- 使用該
filter
函數並使用 Ubound() 檢查結果陣列的大小。 - 用於
Redim preserve
將數組擴展 1 並添加最近檢查的文字值
這是一個一般的小例子
Sub IsInArray()
Dim arrCheck()
arrInput = Array("AAA","BBB","CCC","DDD","BBB","CCC","AAA","DDD","AAA")
arrCheck = Array("AAA","BBB","CCC")
For Each Item In arrInput
If UBound(Filter(arrCheck, Item)) = -1 Then
ReDim Preserve arrCheck(UBound(arrCheck) + 1)
arrCheck(UBound(arrCheck)) = Item
End If
Next
End Sub