Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Dynamic Arrays

Under what circumstances, if any, would one want to code

Dim arr(1 to 20, 1 to 10) instead of

Dim arr
Redim arr(1 to 20, 1 to 10)?

Alan Beban
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default Dynamic Arrays

When one is not sure how many elements can be expected in the array,
when he wants to populate the elements during run time.
Just a crude example:- I want to build an arry based on two columns in a
worksheet. I do not know how may of the rows in these column will
actually have data in it. So I first find out how may rows are actually
used during run time and then will Redim the array, then popuate the
array.
Compared to defining an Array with Dim (1000, 1000) so that may array
will never be short of elements,
I will be saving memory space used by the array, number of calculation
which I will be performing on the array elements, by using dymanic
array.

Sharad



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Dynamic Arrays

Sharad wrote:
When one is not sure how many elements can be expected in the array,
when he wants to populate the elements during run time.
Just a crude example:- I want to build an arry based on two columns in a
worksheet. I do not know how may of the rows in these column will
actually have data in it. So I first find out how may rows are actually
used during run time and then will Redim the array, then popuate the
array.
Compared to defining an Array with Dim (1000, 1000) so that may array
will never be short of elements,
I will be saving memory space used by the array, number of calculation
which I will be performing on the array elements, by using dymanic
array.

Sharad



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Thanks for the response, Sharad. I get from the above why one would use

Dim arr
Redim arr(20,10)

My question is when, if ever, would one want to use

Dim arr(20,10) instead.

Thanks again,
Alan Beban
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default Dynamic Arrays


When one knows number of elements before hand and need not change number
of elements it during the run time and also not want to clear all
elements during run time.

Please note apart from changing the number of elements, Redim can be
used to clear all the elements of the array, by single, Redim command
without using Preserve.

Sharad

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Dynamic Arrays

There's probably some performance benefit because the compiler/OS
doesn't have to worry about potentially having to resize the array. In
the old mainframe days, there was a way to convince the compiler to
stick the entire array into the stack. Helped speed up a program at
the cost of locking down that much memory.

With modern computers, it probably saves a whole microsecond or two
over the lifetime of a program. {grin}

And, it requires fewer keystrokes, something else that seems to impress
many people.

Finally, an oddity. The Dim part is not necessary. While the
documentation says it is, it is not. Until someone pointed it out,
I've been using Redim without a preceeding Dim blissfully unaware of
the requirement.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Under what circumstances, if any, would one want to code

Dim arr(1 to 20, 1 to 10) instead of

Dim arr
Redim arr(1 to 20, 1 to 10)?

Alan Beban



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Dynamic Arrays

Tushar Mehta wrote:
There's probably some performance benefit because the compiler/OS
doesn't have to worry about potentially having to resize the array. In
the old mainframe days, there was a way to convince the compiler to
stick the entire array into the stack. Helped speed up a program at
the cost of locking down that much memory.

With modern computers, it probably saves a whole microsecond or two
over the lifetime of a program. {grin}

And, it requires fewer keystrokes, something else that seems to impress
many people.

Finally, an oddity. The Dim part is not necessary. While the
documentation says it is, it is not. Until someone pointed it out,
I've been using Redim without a preceeding Dim blissfully unaware of
the requirement.

Thanks, Tushar.

I have a dim recollection (no pun intended) that I had the same view
about no Dim being required in xl5, but that something changed in xl97;
that under some circumstances a ReDim would be rejected without a Dim.
I'll try to recreate the problem, and if I do I'll report back.

Thanks again,
Alan Beban
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Dynamic Arrays

Alan Beban wrote:
Tushar Mehta wrote:

There's probably some performance benefit because the compiler/OS
doesn't have to worry about potentially having to resize the array.
In the old mainframe days, there was a way to convince the compiler to
stick the entire array into the stack. Helped speed up a program at
the cost of locking down that much memory.

With modern computers, it probably saves a whole microsecond or two
over the lifetime of a program. {grin}

And, it requires fewer keystrokes, something else that seems to
impress many people.

Finally, an oddity. The Dim part is not necessary. While the
documentation says it is, it is not. Until someone pointed it out,
I've been using Redim without a preceeding Dim blissfully unaware of
the requirement.

Thanks, Tushar.

I have a dim recollection (no pun intended) that I had the same view
about no Dim being required in xl5, but that something changed in xl97;
that under some circumstances a ReDim would be rejected without a Dim.
I'll try to recreate the problem, and if I do I'll report back.

Thanks again,
Alan Beban


Aha!

I have a 1993-94 Microsoft Excel Visual Basic User's Guide. It states
"When you create a dynamic array that is local to a procedure, declaring
the array with a *Dim* or *Static* statement is recommended but not
required."

In xl2000 the following works; but with the Dim statement commented out,
it throws a compile error "Invalid ReDim".

Alzheimer's hasn't caught me yet :-)

Sub foo()
Dim rng As Range
Dim arr
Set rng = Range("A1:C3")
arr = rng
ReDim arr(4, 4)
Debug.Print UBound(arr); UBound(arr, 2)
End Sub

Alan Beban
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Dynamic Arrays

In article ,
says...
Alan Beban wrote:
Tushar Mehta wrote:

There's probably some performance benefit because the compiler/OS
doesn't have to worry about potentially having to resize the array.
In the old mainframe days, there was a way to convince the compiler to
stick the entire array into the stack. Helped speed up a program at
the cost of locking down that much memory.

With modern computers, it probably saves a whole microsecond or two
over the lifetime of a program. {grin}

And, it requires fewer keystrokes, something else that seems to
impress many people.

Finally, an oddity. The Dim part is not necessary. While the
documentation says it is, it is not. Until someone pointed it out,
I've been using Redim without a preceeding Dim blissfully unaware of
the requirement.

Thanks, Tushar.

I have a dim recollection (no pun intended) that I had the same view
about no Dim being required in xl5, but that something changed in xl97;
that under some circumstances a ReDim would be rejected without a Dim.
I'll try to recreate the problem, and if I do I'll report back.

Thanks again,
Alan Beban


Aha!

I have a 1993-94 Microsoft Excel Visual Basic User's Guide. It states
"When you create a dynamic array that is local to a procedure, declaring
the array with a *Dim* or *Static* statement is recommended but not
required."

In xl2000 the following works; but with the Dim statement commented out,
it throws a compile error "Invalid ReDim".

Alzheimer's hasn't caught me yet :-)

Sub foo()
Dim rng As Range
Dim arr
Set rng = Range("A1:C3")
arr = rng
ReDim arr(4, 4)
Debug.Print UBound(arr); UBound(arr, 2)
End Sub

Alan Beban

Hi Alan,

I guess I haven't run into that problem because I never program without
Option Explicit.

The following works just fine:

Option Explicit
Sub testIt()
ReDim arr(1 To 5)
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
arr = Range("A1:A3")
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
arr = Application.WorksheetFunction.Transpose(Range("A1: A3"))
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
End Sub


nbrDim is a function that returns the number of dimensions in an array:

Function nbrDim(x) As Integer
Dim i As Integer, rslt As Integer
i = 1
On Error GoTo XIT
Do While True
rslt = LBound(x, i)
i = i + 1
Loop
XIT:
nbrDim = i - 1
End Function


--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Dynamic Arrays

Hi Alan,

I guess I haven't run into that problem because I never program without
Option Explicit.

The following works just fine:

Option Explicit
Sub testIt()
ReDim arr(1 To 5)
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
arr = Range("A1:A3")
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
arr = Application.WorksheetFunction.Transpose(Range("A1: A3"))
MsgBox TypeName(arr) & ", " & nbrDim(arr) _
& ", " & LBound(arr) & ", " & UBound(arr)
End Sub


nbrDim is a function that returns the number of dimensions in an array:

Function nbrDim(x) As Integer
Dim i As Integer, rslt As Integer
i = 1
On Error GoTo XIT
Do While True
rslt = LBound(x, i)
i = i + 1
Loop
XIT:
nbrDim = i - 1
End Function


--
Regards,

Tushar Mehta


Tushar,

Hope it's not picky to point out your example would fail in XL97

ReDim arr(1 To 5)
arr = Range("A1:A3")
'compile error, can't assign to array (xl97)

but this is OK in xl97
Dim arr
arr = Range("A1:A3")

recently http://tinyurl.com/4ydfd

Alan,

Another highly contrived scenario when it is necessary to declare before
Redim

Sub test()
Dim x, i
Dim arr() As Long 'fails if commented
x = 20
For i = 1 To x
If i = 5 Then Exit For
ReDim Preserve arr(1 To i)
arr(i) = i
Next
MsgBox UBound(arr)
End Sub

Regards,
Peter


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
Dynamic Arrays chaz Excel Discussion (Misc queries) 1 May 23rd 06 12:43 AM
Dynamic arrays Driver New Users to Excel 3 November 7th 05 10:11 PM
Dynamic Arrays Chiba Excel Worksheet Functions 2 July 9th 05 03:58 AM
Dynamic Arrays and Formulae Joseph[_52_] Excel Programming 0 November 18th 04 11:31 AM
dynamic arrays in excel Koos Excel Programming 2 January 10th 04 02:30 PM


All times are GMT +1. The time now is 06:29 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"