PC Review


Reply
Thread Tools Rate Thread

Defining array size when it is dynamic

 
 
Mark Stephens
Guest
Posts: n/a
 
      1st Jul 2008
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 With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      1st Jul 2008
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
 
Reply With Quote
 
XP
Guest
Posts: n/a
 
      1st Jul 2008
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 With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      1st Jul 2008
> 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" <(E-Mail Removed)> wrote in message
news:0572D711-05DB-4B3D-A00A-(E-Mail Removed)...
> 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 With Quote
 
XP
Guest
Posts: n/a
 
      1st Jul 2008

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" <(E-Mail Removed)> wrote in message
> news:0572D711-05DB-4B3D-A00A-(E-Mail Removed)...
> > 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 With Quote
 
Mark Stephens
Guest
Posts: n/a
 
      1st Jul 2008
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" <(E-Mail Removed)> wrote in message
news:BDF02D60-6658-442A-977F-(E-Mail Removed)...
>
> 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" <(E-Mail Removed)> wrote in message
>> news:0572D711-05DB-4B3D-A00A-(E-Mail Removed)...
>> > 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Dynamic Array from list that may change in size dean.brunne@lion-nathan.com.au Microsoft Excel Programming 8 26th Apr 2007 05:09 AM
how to dynamic assign two dimensions array size =?Utf-8?B?Sm9l?= Microsoft ASP .NET 1 15th Mar 2007 06:57 AM
String Array with Dynamic Size Ivan Weiss Microsoft VB .NET 2 19th Nov 2003 09:24 PM
Dynamic Array size Vijay Balki Microsoft VB .NET 1 12th Sep 2003 12:08 AM
size of multidimensional dynamic array ThatFella Microsoft Excel Programming 4 3rd Sep 2003 11:53 PM


Features
 

Advertising
 

Newsgroups
 


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