You can't assign a Variant created with the Array function to a statically
declared array. You can, however, assign it to a dynamic array. For example,
Dim Arr() As Variant
Arr = Array(1, 2, 3, 4)
You can initialize the size of the array with a ReDim, but that has no
effect. The final result will be sized to the number of elements in the
Array statement. E.g
Dim Arr() As Variant
ReDim Arr(0 To 2) '<<<< THIS LINE IS IRRELEVANT
Arr = Array(1, 2, 3, 4)
Note that you need to declare the Arr variable () As Variant rather than,
for example, () As Integer.
--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
"RyanH" <(E-Mail Removed)> wrote in message
news:7746F0E2-B456-4677-8AC2-(E-Mail Removed)...
> Why am I getting this error "Can't Assign To Array" for this code?
>
> Dim myArray(0 To 5) As Single
> Dim Var as Single
>
> ERROR => myArray = Array(var1, var2, var3, var4, var5, var6)
>
> For Each Var In myArray
> Var = 0
> Next Var
>
> but if I code it this way everything works:
>
> Dim myArray As Variant
> Dim Var as Variant
>
> ERROR => myArray = Array(var1, var2, var3, var4, var5, var6)
>
> For Each Var In myArray
> Var = 0
> Next Var
>
> Any help would be great.
> --
> Cheers,
> Ryan