View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default Handling ubound on an uninitialised array

Rob,

if you want to dump a simple array afaik excel doesnt care about upper
and lower bounds.


if you do want to change the upper and lower boundaries
(while keeping the same number of elements AND preserving the data..
the variable must a declared as a variant not a variant array,
and you could do a simple redim preserve.

I'll try to illustrate:


Sub Redimming()
Dim i, arr(), var, cpy

ReDim var(10 To 19)
ReDim arr(10 To 19)
For i = LBound(arr) To UBound(arr)
var(i) = i
arr(i) = i
Next

[a1:a5].Clear
'dump the 10-based VARIANT
[a1].Resize(1, UBound(var) - LBound(var) + 1) = var
'dump the 10-based VARIANT ARRAY
[a2].Resize(1, UBound(arr) - LBound(arr) + 1) = arr

'NOTE REDIM PRESERVE BOTH BOUNDS WORKS FOR A 'PLAIN' VARIANT ONLY
'redim, dump the 1-based
ReDim Preserve var(1 To UBound(var) - LBound(var) + 1)
[a3].Resize(1, UBound(var) - LBound(var) + 1) = var
'redim, dump the 0-based
ReDim Preserve var(0 To UBound(var) - LBound(var))
[a4].Resize(1, UBound(var) - LBound(var) + 1) = var

'Copying ARR to a variant... and THEN resizing works
cpy = arr
ReDim Preserve cpy(0 To UBound(cpy) - LBound(cpy))
[a5].Resize(1, UBound(cpy) - LBound(cpy) + 1) = cpy

'resizing ARR will not work
ReDim Preserve arr(0 To UBound(cpy) - LBound(cpy))

End Sub







--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Rob van Gelder wrote :

It's tempting to argue this further, but I don't think I could
provide a strong case to using zero-based indexing.
I've only ever used zero-based - I've read no studies on bugcount
for/against zero-based. I'm just not the guy to push the point :)

The only thing I could think of is reusing the index variable.. eg.
Copying your x array to a range might require two indexes where
zero-based requires just one.