ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding to Array (https://www.excelbanter.com/excel-programming/445080-adding-array.html)

Charlotte E.[_3_]

Adding to Array
 
I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE

Jim Cone[_2_]

Adding to Array
 
Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(free and commercial excel programs)



"Charlotte E."
wrote in message
...
I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE




joeu2004[_2_]

Adding to Array
 
"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)

PS: If this is in a loop, I don't know how inefficient it is to increase an
array by just one. I usually increase by larger amounts, e.g.
2*UBound(PWL), and maintain variables, e.g. nPWL and nPWLmax, to track the
"active region" of the array. If/when it comes time to copy the array to a
worksheet, I would use Range("a1").Resize(,nPWL) = PWL.


Jim Cone[_2_]

Adding to Array
 

Good points.
I tend to limit the depth of a newsgroup response to the corresponding effort/detail put into the
question.
Jim Cone


"joeu2004"
wrote in message
...
"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)

PS: If this is in a loop, I don't know how inefficient it is to increase an array by just one. I
usually increase by larger amounts, e.g. 2*UBound(PWL), and maintain variables, e.g. nPWL and
nPWLmax, to track the "active region" of the array. If/when it comes time to copy the array to a
worksheet, I would use Range("a1").Resize(,nPWL) = PWL.




Auric__

Adding to Array
 
joeu2004 wrote:

"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)


No need to specify the lower bound in this case:
ReDim Preserve PWL(UBound(PWL)+1)

--
Error -53. We'd tell you what that means, but we don't know either.

Charlotte E.[_3_]

Adding to Array
 
And that concludes my lecture on arrays for today - thanks guys :-)

CE


Den 23.10.2011 17:37, Charlotte E. skrev:
I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE



All times are GMT +1. The time now is 09:41 AM.

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