Make ArrayOne = ArrayTwo

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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()
 
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)
 
This works for me

ItemDataTmp = ItemData

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
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)
 
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
 
OK, thanks for clearing that up.

RBS


Alan Beban said:
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top