array: subscript out of range
Make the array dynamic like this... Note the word preserve will keep you from
losing the contents of the array when it is resized...
Dim strInText As String
Dim strArray() As String
strSourceFile = "C:\ShortTest.txt"
lngInputFile = FreeFile
Open strSourceFile For Input As lngInputFile
iCount = 0
redim strArray(iCount + 1)
While Not EOF(lngInputFile)
Line Input #lngInputFile, strInText
strArray = Split(strInTest, "~")
Debug.Print Trim(strArray(iCount)) ' [just seeing if the data are
being read]
iCount = iCount + 1
redim preserve strArray(iCount + 1)
Wend
Close lngInputFile
"rachel" wrote:
Thanks for the "Option Explicit" reminder. I know i should always do that.
Sigh...
I didn't specify the size of the array because ultimately i'm planning on
using it to import text files with varying number of fields. I don't want to
specify a size that might change.
rachael
"Jim Thomlinson" wrote:
You have not specified the size of your array. Try this to see if that is
your problem. If so we can look at a dynamically sized array...
Dim strArray(100) As String
"rachel" wrote:
I'm playing around with code to import data from a huge delimited text file.
Since i'm learning as i go, i'm trying to take the code in pieces. Below is
what i have so far.
--------------
Dim strInText As String
Dim strArray() As String
strSourceFile = "C:\ShortTest.txt"
lngInputFile = FreeFile
Open strSourceFile For Input As lngInputFile
iCount = 0
While Not EOF(lngInputFile)
Line Input #lngInputFile, strInText
strArray = Split(strInTest, "~")
Debug.Print Trim(strArray(iCount)) ' [just seeing if the data are
being read]
iCount = iCount + 1
Wend
Close lngInputFile
--------------
I get a "subscript out of range" error on the Debug.Print line. When i move
the cursor over strInText in the Line Input # line, it shows the first line
from the text file as I would expect it to look. But when i move the cursor
over the strInText in the strArray=Split line, it says = Empty. I'm guessing
this is why i get the "subscript out of range" error.
I don't know what could be causing this, but again, i'm learning as i go.
Help!
thanks
rachael
|