我的问题是,什么可能导致此错误(错误"下标超出范围。")?我在VBA中运行一个函数,该函数从每分钟更新的特定CSV中读取数据。我的职责是:
Function CSVtoDataArray(CSVpath As String, fileName As String) 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 the_array() As String
Dim file_path As String
whole_file = ""
file_path = CSVpath & fileName
' Load the file.
fnum = FreeFile
Open file_path For Input As fnum
whole_file = Input$(LOF(fnum), #fnum)
Close fnum
' Break the file into lines.'
lines = Split(whole_file, vbCrLf)
whole_file = ""
num_rows = UBound(lines)
the_array = Split(lines(num_rows - 1), ",")
CSVtoDataArray = the_array()
End Function
我的脚本开始运行后错误弹出一段时间,调试标记错误以到达这些行:
num_rows = UBound(lines)
the_array = Split(lines(num_rows - 1), ",")
这表明我的字符串whole_file
为空,给num_rows
的值为-1。为什么你认为是问题?为什么这个错误只在一段时间后发生而不是立即发生?