ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Make ArrayOne = ArrayTwo (https://www.excelbanter.com/excel-programming/400464-make-arrayone-%3D-arraytwo.html)

Mike H.

Make ArrayOne = ArrayTwo
 
What do I have to do to allow this statement to compile. I want to assign
all the elements in one array to the other array. Obviously the "long" way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()

Chip Pearson

Make ArrayOne = ArrayTwo
 
I believe that you need to use a loop through each element. I don't think
you can assign one array to another array, if both are declared as actual
arrays. E.g.,

Dim A1(1 To 3)
Dim A2(1 To 3)
A1 = A2 '<<<< ERROR

will not work. If, however, you are using Variants that contain arrays, you
can set one to the other. E.g.,

Dim V1 As Variant
Dim V2 As Variant
V1 = Array(1, 2, 3)
V2 = Array(4, 5, 6)
V1 = V2 '<<< OK


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Mike H." wrote in message
...
What do I have to do to allow this statement to compile. I want to assign
all the elements in one array to the other array. Obviously the "long"
way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()



Bob Phillips

Make ArrayOne = ArrayTwo
 
This works for me

ItemDataTmp = ItemData

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Mike H." wrote in message
...
What do I have to do to allow this statement to compile. I want to assign
all the elements in one array to the other array. Obviously the "long"
way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()




Chip Pearson

Make ArrayOne = ArrayTwo
 
I should amend my reply to say that if the arrays are dynamic (no bounds
specification in the Dim statement, e.g., Dim A1() As Long) you can assign
one array to another. The error occurs if the arrays are static.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Chip Pearson" wrote in message
...
I believe that you need to use a loop through each element. I don't think
you can assign one array to another array, if both are declared as actual
arrays. E.g.,

Dim A1(1 To 3)
Dim A2(1 To 3)
A1 = A2 '<<<< ERROR

will not work. If, however, you are using Variants that contain arrays,
you can set one to the other. E.g.,

Dim V1 As Variant
Dim V2 As Variant
V1 = Array(1, 2, 3)
V2 = Array(4, 5, 6)
V1 = V2 '<<< OK


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Mike H." wrote in message
...
What do I have to do to allow this statement to compile. I want to
assign
all the elements in one array to the other array. Obviously the "long"
way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()




RB Smissaert

Make ArrayOne = ArrayTwo
 
Sub test()

Dim i As Long
Dim arr1() As Long
Dim arr2() As Long

ReDim arr1(1 To 10) As Long

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub


As Chip said you can assign directly if the arrays are declared dynamically,
not static.


RBS

"Mike H." wrote in message
...
What do I have to do to allow this statement to compile. I want to assign
all the elements in one array to the other array. Obviously the "long"
way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()



RB Smissaert

Make ArrayOne = ArrayTwo
 
OK, thanks for clearing that up.

RBS


"Alan Beban" wrote in message
...
It's slightly more complicated than you or Chip articulated. (1)it's
neither required that the arrays be declared dynamically

Sub test()

Dim i As Long
Dim arr1(1 To 10)
Dim arr2()

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub

works; nor (2) sufficient that the arrays be declared dynamically

Sub test()

Dim i As Long
Dim arr1()
Dim arr2() As Integer

ReDim arr1(1 To 10)

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub

doesn't. Only the array being assigned must be declared dynamically, and
the arrays must be of the same type.

Alan Beban

RB Smissaert wrote:
Sub test()

Dim i As Long
Dim arr1() As Long
Dim arr2() As Long

ReDim arr1(1 To 10) As Long

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub


As Chip said you can assign directly if the arrays are declared
dynamically, not static.


RBS

"Mike H." wrote in message
...

What do I have to do to allow this statement to compile. I want to
assign
all the elements in one array to the other array. Obviously the "long"
way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()



Alan Beban[_2_]

Make ArrayOne = ArrayTwo
 
It's slightly more complicated than you or Chip articulated. (1)it's
neither required that the arrays be declared dynamically

Sub test()

Dim i As Long
Dim arr1(1 To 10)
Dim arr2()

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub

works; nor (2) sufficient that the arrays be declared dynamically

Sub test()

Dim i As Long
Dim arr1()
Dim arr2() As Integer

ReDim arr1(1 To 10)

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub

doesn't. Only the array being assigned must be declared dynamically, and
the arrays must be of the same type.

Alan Beban

RB Smissaert wrote:
Sub test()

Dim i As Long
Dim arr1() As Long
Dim arr2() As Long

ReDim arr1(1 To 10) As Long

For i = 1 To 10
arr1(i) = i
Next i

arr2 = arr1

For i = 1 To 10
Debug.Print arr2(i)
Next i

End Sub


As Chip said you can assign directly if the arrays are declared
dynamically, not static.


RBS

"Mike H." wrote in message
...

What do I have to do to allow this statement to compile. I want to
assign
all the elements in one array to the other array. Obviously the
"long" way
would be to assign element by element, but is there a faster way?

Let ItemDataTmp() = ItemData()





All times are GMT +1. The time now is 05:12 PM.

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