ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Array Element question (https://www.excelbanter.com/excel-programming/323864-array-element-question.html)

S G Booth

Array Element question
 
How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.



Tom Ogilvy

Array Element question
 
Assume a 1 dimensional array

Sub Arr1()
Dim myArray()
Dim num As Long
num = 5
ReDim myArray(0 To num - 1)
For i = 0 To num - 1
myArray(i) = i
Next
lastrow = 20
If UBound(myArray) - LBound(myArray) + 1 < 20 Then _
lastrow = UBound(myArray) - LBound(myArray) + 1
Range("A1:A" & lastrow).Value = Application.Transpose(myArray)
End Sub


If more than 20 values, then only the first 20 values will appear.

--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.





S G Booth

Array Element question
 
Many thanks.
Should have said that the array elements need to paste
with a blank row between each element, rather than as a
contiguous 'list'.

I will be pasting them into a sheet where there will be 45+
designated rows. Any current values can be overwritten. I don't want to
insert the values (creating extra rows), just to overwrite the existing
contents.

Looked in Help to see if there is an argument with
Transpose that might be useful....but there doesn't seem
to be a listing for Transpose.

Can I loop through the 20 elements one at a time, and paste each time with
an offset, ....or a better way?

Regards and thanks.

"Tom Ogilvy" wrote in message
...
Assume a 1 dimensional array

Sub Arr1()
Dim myArray()
Dim num As Long
num = 5
ReDim myArray(0 To num - 1)
For i = 0 To num - 1
myArray(i) = i
Next
lastrow = 20
If UBound(myArray) - LBound(myArray) + 1 < 20 Then _
lastrow = UBound(myArray) - LBound(myArray) + 1
Range("A1:A" & lastrow).Value = Application.Transpose(myArray)
End Sub


If more than 20 values, then only the first 20 values will appear.

--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.







Tom Ogilvy

Array Element question
 
Should have said that the array elements need to paste
with a blank row between each element, rather than as a
contiguous 'list'.


That would have been useful.

Dim i as Long, j as Long, k as Long
Dim rng as Range
' set rng to first cell to hold value
set rng = Worksheets("ABC").Range("F32")
i = -1
j = 0
for k = lbound(vArr) to ubound(vArr)
i = i + 2: j = j + 1
rng(i).Value = vArr(k)
if j = 20 then exit for
Next

code is untested, but should represent a feasible approach.

--
Regards,
Tom Ogilvy


--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
Many thanks.
Should have said that the array elements need to paste
with a blank row between each element, rather than as a
contiguous 'list'.

I will be pasting them into a sheet where there will be 45+
designated rows. Any current values can be overwritten. I don't want to
insert the values (creating extra rows), just to overwrite the existing
contents.

Looked in Help to see if there is an argument with
Transpose that might be useful....but there doesn't seem
to be a listing for Transpose.

Can I loop through the 20 elements one at a time, and paste each time with
an offset, ....or a better way?

Regards and thanks.

"Tom Ogilvy" wrote in message
...
Assume a 1 dimensional array

Sub Arr1()
Dim myArray()
Dim num As Long
num = 5
ReDim myArray(0 To num - 1)
For i = 0 To num - 1
myArray(i) = i
Next
lastrow = 20
If UBound(myArray) - LBound(myArray) + 1 < 20 Then _
lastrow = UBound(myArray) - LBound(myArray) + 1
Range("A1:A" & lastrow).Value = Application.Transpose(myArray)
End Sub


If more than 20 values, then only the first 20 values will appear.

--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.









S G Booth

Array Element question
 
Sorry for that omission. Didn't know Transpose.
Ignorantly, I assumed any reply would pull out the
array elements one at a time (and I could then try
to adapt the code).

Thanks for the approach. Will try to develop it.

Regards.

"Tom Ogilvy" wrote in message
...
Should have said that the array elements need to paste
with a blank row between each element, rather than as a
contiguous 'list'.


That would have been useful.

Dim i as Long, j as Long, k as Long
Dim rng as Range
' set rng to first cell to hold value
set rng = Worksheets("ABC").Range("F32")
i = -1
j = 0
for k = lbound(vArr) to ubound(vArr)
i = i + 2: j = j + 1
rng(i).Value = vArr(k)
if j = 20 then exit for
Next

code is untested, but should represent a feasible approach.

--
Regards,
Tom Ogilvy


--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
Many thanks.
Should have said that the array elements need to paste
with a blank row between each element, rather than as a
contiguous 'list'.

I will be pasting them into a sheet where there will be 45+
designated rows. Any current values can be overwritten. I don't want to
insert the values (creating extra rows), just to overwrite the existing
contents.

Looked in Help to see if there is an argument with
Transpose that might be useful....but there doesn't seem
to be a listing for Transpose.

Can I loop through the 20 elements one at a time, and paste each time
with
an offset, ....or a better way?

Regards and thanks.

"Tom Ogilvy" wrote in message
...
Assume a 1 dimensional array

Sub Arr1()
Dim myArray()
Dim num As Long
num = 5
ReDim myArray(0 To num - 1)
For i = 0 To num - 1
myArray(i) = i
Next
lastrow = 20
If UBound(myArray) - LBound(myArray) + 1 < 20 Then _
lastrow = UBound(myArray) - LBound(myArray) + 1
Range("A1:A" & lastrow).Value = Application.Transpose(myArray)
End Sub


If more than 20 values, then only the first 20 values will appear.

--
Regards,
Tom Ogilvy

"S G Booth" wrote in message
...
How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.











James W.[_2_]

Array Element question
 
To answer your second question...

This will rename the first worksheet to 'Test'

Sheet1.Name = "Test"


"S G Booth" wrote:

How can I refer to elements in an array?
Say just the 1st 20, and paste the values into
"A1" of the activesheet, please?

What if there were less than 20 elements?

Regards.





All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com