LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Assign a single variable to an Array filled with same data typ

You could always do something like this:


Sub test()

Dim i As Long
Dim arrVariant
Dim arrSingles(0 To 3) As Single

arrVariant = Array(1.1, 2.2, 3.3, 4.4)

For i = 0 To 3
arrSingles(i) = arrVariant(i)
Next i

'just to show you still have a Single data type
MsgBox VarType(arrSingles(1)), , "VarType 4 = vbSingle"

End Sub


RBS


"RyanH" wrote in message
...
You have to declare the array as variant, even though all the arguments in
the array have the same data type ( in my case Single)?
--
Cheers,
Ryan


"Rick Rothstein (MVP - VB)" wrote:

First off, the Array function returns an array and, in VB, arrays can
only
be assigned to other arrays if those other arrays were dynamically
declared.
You declared your array as having a fixed number of elements, so the
array
assignment failed. On top of that, the Array function requires its target
to
be a Variant (a variant array is also possible target) where as you tried
specifying it as a Single. The reason a Variant target is required is
because you can put almost anything in the argument list to the Array
function (as long as you remember what is what, of course<g). For
example...

Dim V As Variant
V = Array(123, "Text String", Range("A1"))
MsgBox V(0) & vbCrLf & V(1) & vbCrLf & V(2).Address

Notice that the 3rd element, V(2), is a Range object, so to MessageBox
out
something from it, you need to reference one of its properties. True, I
could have let it use its default Value property, but I wanted to
positively
demonstrate that it was an actual object being stored in the third
element.

Rick


"RyanH" wrote in message
...
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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coverting single-value array into an ordinary variable Fred Chow Excel Programming 2 October 25th 07 04:23 PM
Sun IF two data types are the same in a single column David_Williams_PG () Excel Discussion (Misc queries) 4 September 8th 06 06:24 PM
how do I make single character as a variable in an array Hazlgrnguy Excel Worksheet Functions 1 September 25th 05 08:12 AM
assign objects into array or object variable? Fendic[_12_] Excel Programming 1 July 22nd 05 09:23 PM
VBA - Convert my variable range array to single cell string Kevin G[_2_] Excel Programming 6 January 28th 04 07:20 AM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"