PDA

View Full Version : Setting a Range Equal to an Array



Mcaa51
11-06-2010, 10:15 AM
In order to avoid writing data to the worksheet one cell at a time using a loop I am trying to use the below code. This is not working correctly however, and is writing the first value in the array to the entire range on the worksheet. What am I Doing wrong? Thanks.

Sub GetUnique(wksDataSource As Worksheet, wksDataDest As Worksheet)
Dim saCpty As Variant
Dim saTempArray() As String
Dim iLoopArray As Integer
Dim rngDest As Range

saCpty = UniqueItemList(Range("CNPTY"), True)

ReDim saTempArray(1 To UBound(saCpty))

Set rngDest = wksDataDest.Range(Cells(1, 1), Cells(UBound(saCpty), 1))

For iLoopArray = 1 To UBound(saCpty)
saTempArray(iLoopArray) = saCpty(iLoopArray)
Next iLoopArray

rngDest.Value = saCpty

End Sub

mdmackillop
11-06-2010, 10:35 AM
Try
rngDest.Value = application.transpose(saCpty)

Bob Phillips
11-06-2010, 11:02 AM
What exactly does this sub do? It has an argument wksDataSource that it doesn't use, it loads an array that it does nothing with, and there is a real chance that one item gets missed.

Mcaa51
11-06-2010, 01:11 PM
Thanks mdmckillop that appears to have worked.

xld, you are correct wksDataSource and saTempArray were no longer being used, I just neglected to take them out before posting. Sorry for the confusion

Thanks for the help.

Bob Phillips
11-06-2010, 01:55 PM
What about my point about dropping an item? When I tested it I loaded a 5 item array, but only 4 were dumped as my array was zero-based.