Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Checking array to see if value exists before adding to array | Excel Programming | |||
Adding rows in the array | Excel Programming | |||
Adding values to 2-dim array | Excel Programming | |||
Adding to an array | Excel Programming | |||
Adding an Array | Excel Programming |