Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default 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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default 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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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

Reply
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
Declaring one bidimensinal Array Andoni[_17_] Excel Programming 2 August 21st 04 02:03 AM
Declaring variable as a dynamic array? aiyer[_44_] Excel Programming 1 August 17th 04 11:01 PM
declaring a public array JT[_2_] Excel Programming 3 July 27th 04 11:18 PM
Q: Declaring a dynamic array Srdjan Kovacevic[_4_] Excel Programming 1 January 16th 04 07:24 PM
declaring an array of CheckBox's Didier Poskin Excel Programming 4 September 9th 03 09:02 AM


All times are GMT +1. The time now is 06:00 PM.

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"