ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Declaring Data Type of Array (https://www.excelbanter.com/excel-programming/352435-declaring-data-type-array.html)

[email protected]

Declaring Data Type of Array
 
Hi,

dimensioning a scalar variable and declaring its data
type in VBA is no problem:

Dim a As Double

What if I want the variable to hold an array with multiple
components, all with Double precision? This doesn't seem
to work:

Dim a As Double
a = Array()
ReDim a(2)

Thanks a lot for suggestions!
Tom


Toppers

Declaring Data Type of Array
 
Tom,

Dim a() As Double
ReDim a(10)

For i = LBound(a) To UBound(a)
a(i) = i *i
Debug.Print i, a(i)
Next i


HTH

" wrote:

Hi,

dimensioning a scalar variable and declaring its data
type in VBA is no problem:

Dim a As Double

What if I want the variable to hold an array with multiple
components, all with Double precision? This doesn't seem
to work:

Dim a As Double
a = Array()
ReDim a(2)

Thanks a lot for suggestions!
Tom



keepITcool

Declaring Data Type of Array
 
let me expand on Topper's reply

if you know the bounds beforehand
you create a fixed size array as follows:

dim adFixed(1 to 10) as double
dim adFixed#(1 to 10)

dim adFixed(1 to 10,1 to 4) as double
dim adFixed#(1 to 10,1 to 4)

if you do not specify a lowerboundary
the lowerbound is 0 unless the module has
Option Base 1

e.g.
dim adFixed(10) as double
dim adFixed#(10)


if you need to dynamically dimension your array in code:
you use redim.

redim asDyna$(rowsize,colsize)

initializing the empty array (dim asDyna() as string)
at the beginning of the procedure is not required,
although i always to it for code readability

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Toppers wrote :

Tom,

Dim a() As Double
ReDim a(10)

For i = LBound(a) To UBound(a)
a(i) = i *i
Debug.Print i, a(i)
Next i


HTH

" wrote:

Hi,

dimensioning a scalar variable and declaring its data
type in VBA is no problem:

Dim a As Double

What if I want the variable to hold an array with multiple
components, all with Double precision? This doesn't seem
to work:

Dim a As Double
a = Array()
ReDim a(2)

Thanks a lot for suggestions!
Tom



Toppers

Declaring Data Type of Array
 
Thanks. A more thoughtful reply than my own!

"keepITcool" wrote:

let me expand on Topper's reply

if you know the bounds beforehand
you create a fixed size array as follows:

dim adFixed(1 to 10) as double
dim adFixed#(1 to 10)

dim adFixed(1 to 10,1 to 4) as double
dim adFixed#(1 to 10,1 to 4)

if you do not specify a lowerboundary
the lowerbound is 0 unless the module has
Option Base 1

e.g.
dim adFixed(10) as double
dim adFixed#(10)


if you need to dynamically dimension your array in code:
you use redim.

redim asDyna$(rowsize,colsize)

initializing the empty array (dim asDyna() as string)
at the beginning of the procedure is not required,
although i always to it for code readability

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Toppers wrote :

Tom,

Dim a() As Double
ReDim a(10)

For i = LBound(a) To UBound(a)
a(i) = i *i
Debug.Print i, a(i)
Next i


HTH

" wrote:

Hi,

dimensioning a scalar variable and declaring its data
type in VBA is no problem:

Dim a As Double

What if I want the variable to hold an array with multiple
components, all with Double precision? This doesn't seem
to work:

Dim a As Double
a = Array()
ReDim a(2)

Thanks a lot for suggestions!
Tom




[email protected]

Declaring Data Type of Array
 
Hello,

thanks a lot for your quick and very helpful responses!
There are still two things I am struggling with:

1)

I want to do this:

v = Array()
ReDim v(2)
v(0) = Array(0, 0, 0)

But I cannot see a way to declare the values at the level
of v(0)(0) as Single, Double, etc.

2)

In the following code I dimension an Array of doubles as
suggested.


Sub Main()
Dim foo(0 To 3) As Double
foo = bar()
End Sub


Function bar() As Double
bar = Array(0.1, 0.2, 0.3)
End Function


But I am getting a 'Can't assign to variable' error. If I
can return an array, why is it not possible to assign it
as such?

Your help is very much appreciated!
Tom


Toppers

Declaring Data Type of Array
 
One way:

Dim v() As Double

Vdouble = Array(1.2, 3.4, 6.5)

ReDim v(2)

For i = 0 To 2
v(i) = Vdouble(i)
next i
Next " wrote:

Hello,

thanks a lot for your quick and very helpful responses!
There are still two things I am struggling with:

1)

I want to do this:

v = Array()
ReDim v(2)
v(0) = Array(0, 0, 0)

But I cannot see a way to declare the values at the level
of v(0)(0) as Single, Double, etc.

2)

In the following code I dimension an Array of doubles as
suggested.


Sub Main()
Dim foo(0 To 3) As Double
foo = bar()
End Sub


Function bar() As Double
bar = Array(0.1, 0.2, 0.3)
End Function


But I am getting a 'Can't assign to variable' error. If I
can return an array, why is it not possible to assign it
as such?

Your help is very much appreciated!
Tom



keepITcool

Declaring Data Type of Array
 
i think topper's taken care of the first part of your questions.

Function bar() as double()
'Array(1,2,3) returns a VARIANT,
'which you cant assign to a typed array

dim res#(1 to 3)
res(1)=.1
res(2)=.2
res(3)=.3
bar=res

'note the function is typed as double()
'cant be used in xl97, only newer versions.
end Function


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


wrote :

Hello,

thanks a lot for your quick and very helpful responses!
There are still two things I am struggling with:

1)

I want to do this:

v = Array()
ReDim v(2)
v(0) = Array(0, 0, 0)

But I cannot see a way to declare the values at the level
of v(0)(0) as Single, Double, etc.

2)

In the following code I dimension an Array of doubles as
suggested.


Sub Main()
Dim foo(0 To 3) As Double
foo = bar()
End Sub


Function bar() As Double
bar = Array(0.1, 0.2, 0.3)
End Function


But I am getting a 'Can't assign to variable' error. If I
can return an array, why is it not possible to assign it
as such?

Your help is very much appreciated!
Tom


Tom Ogilvy

Declaring Data Type of Array
 
You can only assign arrays to a variant variable. Otherwise, you can't
assign an array to an array.



Sub Main()
Dim foo(0 To 3) As Variant
foo(0) = bar()
For i = LBound(foo(0)) To UBound(foo(0))
Debug.Print foo(0)(i), TypeName(foo(0)(i))
Next
End Sub


Function bar() As Variant
bar = Array(0.1, 0.2, 0.3)
End Function

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hello,

thanks a lot for your quick and very helpful responses!
There are still two things I am struggling with:

1)

I want to do this:

v = Array()
ReDim v(2)
v(0) = Array(0, 0, 0)

But I cannot see a way to declare the values at the level
of v(0)(0) as Single, Double, etc.

2)

In the following code I dimension an Array of doubles as
suggested.


Sub Main()
Dim foo(0 To 3) As Double
foo = bar()
End Sub


Function bar() As Double
bar = Array(0.1, 0.2, 0.3)
End Function


But I am getting a 'Can't assign to variable' error. If I
can return an array, why is it not possible to assign it
as such?

Your help is very much appreciated!
Tom




[email protected]

Declaring Data Type of Array
 
Thanks a lot for all your help!!!

Tom



All times are GMT +1. The time now is 01:32 PM.

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