View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Can you make Range Equal All Values in an Array?

Dimension with Redim BEFORE you start to populate the array. The point is it
should be a 2d array. Only use Preserve if you need to increase the last
dimension, ie no. of columns in this case.

If your original array needs to be 1d, eg pulled in from some other source,
try something like this

Redim tmpArr(1 to ubound(arr) - lbound(arr) + 1, 1 to 1


for i = 1 to ubound(tmpArr)
tmparr(i,1) = arr(i) ' adjust if lbound(arr) is not 1
next

then assign tmpArr to the coorectly sized range

Regards,
Peter T



"RyanH" wrote in message
...
Thanks for the replys! I used this code and didn't get any errors, but no
data was applied to the Data Storage sheet. Any ideas why?

Don't I have to use Preserve, because if I ReDim the Array it will delete
the data in the array, correct?

Why do I have to resize the column portion of .Cells(1, lngColumn) using
the
Resize method? The cell is already 1 column wide, so I shouldn't have to
specify the column resized width, right?

ReDim ctrlArray(1 To UBound(ctrlArray), 1 To 1)

With Sheets("Data Storage").Cells(1, lngColumn)
.Resize(UBound(ctrlArray), UBound(ctrlArray, 2)).Value = ctrlArray
End With
--
Cheers,
Ryan


"Peter T" wrote:

ctrlArray should be dimensioned as a 2d array

ReDim ctrlArray(1 to NumRows, 1 to 1)

Assuming LBound of each of the array's dimensions is 1, you could do
something like this

With Sheets("Data Storage").Cells(1, lngColumn)
..resize(ubound(ctrlArray), ubound(ctrlArray, 2)).value = ctrlArray
End with

Regards,
Peter T



"RyanH" wrote in message
...
I currently fill a range with array values like this.

' store control values
For i = 1 To UBound(ctrlArray)
Sheets("Data Storage").Cells(i, lngColumn) = ctrlArray(i)
Next i

Is there a way to make this faster? I thought you could use this line
below, but it doesn't seem to work.

Sheets("Data Storage").Range(Cells(1, lngColumn),
Cells(UBound(ctrlArray)
+
1, lngColumn)) = ctrlArray
--
Cheers,
Ryan