Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default Defining array size when it is dynamic

Hi,

At the beginning when you define an array do you have to specify a size or
can you just leave the () empty and then it will hold however many items it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Defining array size when it is dynamic

You can use:

dim myArr() as long 'variant/string...

and redim it when you the dimensions.

redim myArr(1 to 5, 7 to 9, -1 to 1)
(as a weird example)

Mark Stephens wrote:

Hi,

At the beginning when you define an array do you have to specify a size or
can you just leave the () empty and then it will hold however many items it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Defining array size when it is dynamic

Hi,

In this example, the array (saItems --- sa=string array in my shorthand) is
redimensioned dynamically and only adds data from the cells in column three
that are not blank :

Dim saItems() as String
Dim rCell as Range
Dim lX as Long
For Each rCell in ActiveSheet.UsedRange.Columns(3).Rows
If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1
End If
Next

"Preserve" causes the array to hold all items previously loaded into the
array; without this keyword, the array would only hold one item, i.e. the
last one loaded.

HTH

"Mark Stephens" wrote:

Hi,

At the beginning when you define an array do you have to specify a size or
can you just leave the () empty and then it will hold however many items it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Defining array size when it is dynamic

If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1


A ReDim Preserve operation is expensive. A better way is to ReDim the array
to the largest possible size before doing anything with the array, fill the
array with the apporpriate values, and then do a single ReDim Preserve at
the end to reduce the size of the array to the actual used size.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"XP" wrote in message
...
Hi,

In this example, the array (saItems --- sa=string array in my shorthand)
is
redimensioned dynamically and only adds data from the cells in column
three
that are not blank :

Dim saItems() as String
Dim rCell as Range
Dim lX as Long
For Each rCell in ActiveSheet.UsedRange.Columns(3).Rows
If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1
End If
Next

"Preserve" causes the array to hold all items previously loaded into the
array; without this keyword, the array would only hold one item, i.e. the
last one loaded.

HTH

"Mark Stephens" wrote:

Hi,

At the beginning when you define an array do you have to specify a size
or
can you just leave the () empty and then it will hold however many items
it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark




  #5   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Defining array size when it is dynamic


Thanks for the tip. I didn't know about the cost...

Relatively speaking, how costly is it?

Going further and given the example, is it less costly to dim the array to
65536 right up front, or to redim on the fly (as I did), or to count the
number of blanks first, then dim the array once (even though it would require
a little more code)?



"Chip Pearson" wrote:

If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1


A ReDim Preserve operation is expensive. A better way is to ReDim the array
to the largest possible size before doing anything with the array, fill the
array with the apporpriate values, and then do a single ReDim Preserve at
the end to reduce the size of the array to the actual used size.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"XP" wrote in message
...
Hi,

In this example, the array (saItems --- sa=string array in my shorthand)
is
redimensioned dynamically and only adds data from the cells in column
three
that are not blank :

Dim saItems() as String
Dim rCell as Range
Dim lX as Long
For Each rCell in ActiveSheet.UsedRange.Columns(3).Rows
If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1
End If
Next

"Preserve" causes the array to hold all items previously loaded into the
array; without this keyword, the array would only hold one item, i.e. the
last one loaded.

HTH

"Mark Stephens" wrote:

Hi,

At the beginning when you define an array do you have to specify a size
or
can you just leave the () empty and then it will hold however many items
it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default Defining array size when it is dynamic

Dear Chip, Dave and HTH,

Thank you all for your help, Iit has all come folooding back and I now feel
competent with arrays again. FYI I am going to adopt a combination of all
your suggestions and count the number of cells in the column (which is the
number of items in my single dimension array) and then do one redim of the
array, thank you all once more, kind regards, Mark


"XP" wrote in message
...

Thanks for the tip. I didn't know about the cost...

Relatively speaking, how costly is it?

Going further and given the example, is it less costly to dim the array to
65536 right up front, or to redim on the fly (as I did), or to count the
number of blanks first, then dim the array once (even though it would
require
a little more code)?



"Chip Pearson" wrote:

If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1


A ReDim Preserve operation is expensive. A better way is to ReDim the
array
to the largest possible size before doing anything with the array, fill
the
array with the apporpriate values, and then do a single ReDim Preserve at
the end to reduce the size of the array to the actual used size.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"XP" wrote in message
...
Hi,

In this example, the array (saItems --- sa=string array in my
shorthand)
is
redimensioned dynamically and only adds data from the cells in column
three
that are not blank :

Dim saItems() as String
Dim rCell as Range
Dim lX as Long
For Each rCell in ActiveSheet.UsedRange.Columns(3).Rows
If rCell.FormulaR1C1 < "" Then
lX = lX + 1
Redim Preserve saItems(lX)
saItems(lX) = rCell.FormulaR1C1
End If
Next

"Preserve" causes the array to hold all items previously loaded into
the
array; without this keyword, the array would only hold one item, i.e.
the
last one loaded.

HTH

"Mark Stephens" wrote:

Hi,

At the beginning when you define an array do you have to specify a
size
or
can you just leave the () empty and then it will hold however many
items
it
holds (or can you redim it when you know). A bit rusty on arrays, help
appreciatyed, regards, Mark






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
Defining Font Size in VBA CherylH Excel Discussion (Misc queries) 2 August 13th 07 06:48 PM
Creating a Dynamic Array from list that may change in size [email protected] Excel Programming 4 April 26th 07 05:09 AM
help defining dynamic range joecrabtree Charts and Charting in Excel 0 December 6th 06 03:33 PM
Defining Dynamic Range Jamie[_11_] Excel Programming 4 October 27th 05 06:36 PM
size of multidimensional dynamic array ThatFella[_2_] Excel Programming 4 September 3rd 03 11:53 PM


All times are GMT +1. The time now is 05:41 AM.

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

About Us

"It's about Microsoft Excel"