ExcelBanter

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

Otto Moehrbach[_6_]

Adding to an array
 
Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto



Myrna Larson

Adding to an array
 
If you are sure you need both arrays:

Sub Test()
Dim Array1() As String
Dim Array2() As String

ReDim Array1(1 To 3)
For i = 1 To 3
Array1(i) = Format$(i)
Next i

Array2 = Array1
ReDim Preserve Array2(1 To UBound(Array2) + 1)
Array2(4) = "4"

For i = 1 To UBound(Array2())
Debug.Print Array2(i)
Next i
End Sub

You can also copy the values from Array1 to Array2, one element at a time, in
a For/Next loop, i.e.

For i = 1 to Ubound(Array1())
Array2(i) = Array1(i)
Next i

On Sun, 24 Oct 2004 12:52:32 -0400, "Otto Moehrbach"
wrote:

Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto



Myrna Larson

Adding to an array
 
PS: DO you need a 2nd array? As long as the first was defined as a dynamic
array (no subscripts in the DIM statement, just in a REDIM statement), you can
always change the size of the right-most dimension, up or down, without losing
data, with the REDIM PRESERVE statement.

On Sun, 24 Oct 2004 12:52:32 -0400, "Otto Moehrbach"
wrote:

Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto



Bob Phillips[_6_]

Adding to an array
 
Hi Otto,

here is one way

myArray1 = Array("A", "B", "C")
myArray2 = myArray1
ReDim Preserve myArray2(LBound(myArray2) To UBound(myArray2) + 1)
myArray2(UBound(myArray2)) = "D"

--

HTH

RP

"Otto Moehrbach" wrote in message
...
Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto





Otto Moehrbach[_6_]

Adding to an array
 
Myrna, Bob
Thanks for your help. Otto
"Otto Moehrbach" wrote in message
...
Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto




Alan Beban[_2_]

Adding to an array
 
Otto Moehrbach wrote:

Excel 2002, WinXP
Let's say I have an array, MyArray, consisting of A, B, C. (Strings only)
Now lets say that I want to build another array, OtherArray, consisting of
the same A, B, C, but having D as well. I know that I can build the
OtherArray as simply A, B, C, D but I don't want to do that. I want to
build the OtherArray by referencing MyArray and tacking D on to it.
How do I code the building of the OtherArray by using MyArray and D?
Thanks for your help. Otto


For another approach, if the functions in the freely downloadable file
at http://home.pacbell.net/beban are available to your workbook

MyArray2 = MakeArray(MyArray,"D",1)

Alan Beban


All times are GMT +1. The time now is 07:21 AM.

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