0

I am getting a "Subscript out of range error" on line 23 of my code. I am opening and reading a .csv file and trying to return the data of the last line. The input information looks like this:

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 ,

Outside of the while loop if I say WScript.Echo = strValues(3) then it echoes the right data, but if I try the same thing within the loop, then I simply get an out of range error for anything other than strValues(0). I don't understand this because if I check the strValues array once the while loop is complete I can call any of the 7 elements.

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

Thanks

1 Answer 1

2

The first line of your test file does not contain a comma, thus the array strValues has a single entry only during the first iteration.

place an extra strLine = inputFile.ReadLine before the do while loop and you should be fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It makes a lot of sense now.
+1, although I think .SkipLine would be more appropriate when he's ignoring that line anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.