我在代码的第 23 行收到“下标超出范围错误”。我正在打开并读取 .csv 文件并尝试返回最后一行的数据。输入信息如下所示:
Control Unit
Date,Time,Seconds,V1,V2,V3,V4,
07/12/2013 ,11:27:21 ,0 ,Closed ,Closed ,Closed ,Closed ,
07/12/2013 ,11:27:22 ,1 ,Closed ,Closed ,Closed ,Closed ,
如果我说 WScript.Echo = strValues(3) 在 while 循环之外,它会回显正确的数据,但是如果我在循环中尝试相同的操作,那么我只会得到 strValues(0) 以外的任何内容的超出范围错误)。我不明白这一点,因为如果我在 while 循环完成后检查 strValues 数组,我可以调用 7 个元素中的任何一个。
Option Explicit
Dim first, secnd
Dim fso, inputFile
Dim strSource, strLine, strValues
Const ForReading = 1
'Create the file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'Initialize a few items
strSource = "C:\Testing\data.csv"
'Open the source file to read it
Set inputFile = fso.OpenTextFile(strSource,ForReading)
'Read the file line by line
Do while not inputFile.AtEndOfStream
strLine = inputFile.ReadLine
'Split the line on the comma into an array
strValues = Split(strLine, ",")
'Check if the dq number matches
first = strValues(0)
'secnd = strValues(3) 'here it tells me its out of range
Loop
WScript.Echo strLine 'once the loop is over it gives me the values just fine
Wscript.Echo strValues(1)
secnd = strValues(3) 'this gives no error
'Close the file
inputFile.Close
'Clean up
Set inputFile = Nothing
Set fso = Nothing
谢谢