Array UBound: 'Subscript out of range'??

E

Ed

I have declared an array using:
Dim Arydoc(1 To 6, 1 To 50)

I was trying to find the next open slot to write to using:
y = 3
z = UBound(Arydoc, y)
Arydoc(y, z + 1) = strAry
but the UBound generates an error "Subscript out of range". Nothing has
been written into the array yet, so I though this would have returned a "z"
of 1 (I'm using Option Base 1), and I would write to Arydoc(3,1).

What am I doing wrong?

Ed
 
J

Jonathan West

Ed said:
I have declared an array using:
Dim Arydoc(1 To 6, 1 To 50)

I was trying to find the next open slot to write to using:
y = 3
z = UBound(Arydoc, y)
Arydoc(y, z + 1) = strAry
but the UBound generates an error "Subscript out of range". Nothing has
been written into the array yet, so I though this would have returned a
"z" of 1 (I'm using Option Base 1), and I would write to Arydoc(3,1).

What am I doing wrong?

You are trying to determine the upper bound of the 3rd dimension of a
two-dimensional array.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

Ed

You are trying to determine the upper bound of the 3rd dimension of a
two-dimensional array.
Thanks, Jonathan. Once you pointed that out, it seems obvious.

That still leaves me, though, with trying to find the next open spot in any
"level" of the array. I'm writing terms to the array using a loop process
and a Select Case statement to determine the first term (the "y" of
Arydoc(y,z)). If I have five items written to Arydoc(1, z) and three to
Arydoc(2,z), how *do* I find out what "z" is for each first term so my next
one will write to Arydoc(y, z+1) for each "y"?

Ed
 
J

Jonathan West

Ed said:
Thanks, Jonathan. Once you pointed that out, it seems obvious.

That still leaves me, though, with trying to find the next open spot in
any "level" of the array. I'm writing terms to the array using a loop
process and a Select Case statement to determine the first term (the "y"
of Arydoc(y,z)). If I have five items written to Arydoc(1, z) and three
to Arydoc(2,z), how *do* I find out what "z" is for each first term so my
next one will write to Arydoc(y, z+1) for each "y"?

Let me see if I understand you. You want to fill, at varying rates,
successive elements of the 6 rows of AryDoc, and to know at any time which
is the next unfilled spot.

You need some counters. An obvious place to put the counters is in another
array. Like this. You don't need Ubound - that tells you the size of the
array available, not how much of it has been assigned some value.

Dim Arydoc(1 To 6, 1 To 50)
Dim AryIndex(1 to 6)

y = 3

AryIndex(y) = AryIndex(y) + 1
Arydoc(y, AryIndex(y)) = strAry

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

Ed

Thanks for all your help, Jonathan. I appreciate the time you take to
explain these things.
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top