ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Append to an array (https://www.excelbanter.com/excel-programming/430262-append-array.html)

maweilian

Append to an array
 

This is my best guess on how to do this:

Dim testvalue, testarray()
For testvalue = 50 To 100 Step 5
ReDim Preserve testarray(UBound(testarray) + 1)
testarray(UBound(testarray)) = testvalue
Next testvalue

So the goal is to start with an array of unknown size and simply add values
until complete.

I am sure this is very flawed. Would appreciate any help.

Will


joel

Append to an array
 

What is wrong with your code. It is not flawed.

I found the code below to be useful. You code created an extra item in the
array. When you do redim testarray(1) it really creates two items in the
array. A zero index and a one index. My code makes the aray exactly the
same size as the data that is inside. it really doesn't make a difference
except when you create a UDF function. then it sometimes matters. for
example you returned an array to the worksheet function Average. The average
would not be correct because you would be dviding by one more item then you
really had.

Sub test()

Dim testvalue, testarray

For testvalue = 50 To 100 Step 5
If IsArray(testarray) Then
ReDim Preserve testarray(0 To (UBound(testarray) + 1))
Else
ReDim testarray(0 To 0)
End If
testarray(UBound(testarray)) = testvalue
Next testvalue

End Sub

"maweilian" wrote:

This is my best guess on how to do this:

Dim testvalue, testarray()
For testvalue = 50 To 100 Step 5
ReDim Preserve testarray(UBound(testarray) + 1)
testarray(UBound(testarray)) = testvalue
Next testvalue

So the goal is to start with an array of unknown size and simply add values
until complete.

I am sure this is very flawed. Would appreciate any help.

Will


Bernd P

Append to an array
 
Hello Will,

Nothing wring with your code, I think.

Its just the question how much effort you want to invest into your
approach.

An example I developed you can see he
http://www.sulprobil.com/html/pstat.html

Regards,
Bernd


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

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