Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to copy all the values in an array to cells at once from VBA?

Status
Not open for further replies.

juschuma

Programmer
Oct 23, 2002
18
0
0
US
I have an array called myArray(3).

It's values are myArray("12", "5", "12", "35")

I want to copy each value into a cell on a spreadsheet. Usually I do something like this:

i=0
Do while i < 4
Range(&quot;A1&quot;).Offset(0, i).Formula = myArray(i)
i = i+1
Loop

Is it possible to do this in one line and not use the loop?

 
Hi, juschuma,

You might want to tune up your routine to make more general...
Code:
lRowStart = 1 - LBound(MyArray, 1)
For i = LBound(MyArray, 1) to UBound(MyArray, 1)
   Cells(lRowStart + i, 1).Value = MyArray(i)
Next
There's no other way that I know of. :) Skip,
SkipAndMary1017@mindspring.com
 
Hi juschuma,

You can assign the value of an array to a range, as long as that range has the same dimensions as your array

This is from a bit of my code which dumps the list I use for an Excel combobox in a worksheet:

Dim l_sResultArray() as String
Dim l_lStartRow as Long
Dim l_iStartCol as Integer

'<code to fill the array here ...>

'Dump array in range
Range(Cells(l_lStartRow, l_iStartCol), Cells(l_lStartRow, p_StartCol).Offset(UBound(l_sResultArray) + 2, <the number of columns your array has - 1>)) = l_sResultArray

Hope this helps

Cheers
Nikki
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top