我想從CSV文件中填充單詞之間有空格的數組,以及在我用連字符替換所有空格後嘗試填充的第二個單詞。單詞vba文本替換數組
因此,array1的內容看起來像這樣「你好」,而array2的內容看起來像這樣「你好」。
我試過使用替換(文本,舊的,新的),但我遇到了問題。在沒有其他潛艇的獨立宏中,它是有效的。如果有任何其他的潛艇沒有,嘔吐和錯誤說
「的參數或無效的屬性賦值打錯了」這是我所得到的代碼:
Private Sub import_csv()
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 imported_array() As String
Dim txt As String
Dim R As Long
Dim C As Long
file_name = "test.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)
ReDim imported_array(num_rows, num_cols)
'Copy the data into the arrays
For R = 0 To num_rows
If Len(lines(R)) > 0 Then
one_line = Split(lines(R), ",")
For C = 0 To num_cols
imported_array(R, C) = one_line(C)
txt = one_line(C)
txt = replace(txt, " ", "-") '<----- problem line
the_array(R, C) = txt
Next C
End If
Next R
End Sub
您是否定義了名爲「replace」的函數?在VBA中已經有了一個這樣的名字的方法...... –
我想我找出了問題所在。我嘗試使用VBA.Replace,它的工作。所以我通過引用,但找不到任何丟失:/ –