PC Review


Reply
 
 
don
Guest
Posts: n/a
 
      17th Nov 2008
I would like to build an array by looping thru a set of criteria.
When finished use the array to do some calculations based on the
length of the array.

However when I try to build an array by concatentating the rows found,
ie rownum= rownum & "," & d.row
the result is "11 , 12"
which the array command views as 1 item in the array when I want it to
be 2 items, namely 11 and 12.
the result of msgbox ubound(array(rownum)) is 0
How do I concatenate rows found into an array.

Thanks

 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      17th Nov 2008
This looks at Column A
Sub loopArray()
Dim arrExcelValues()
Dim rng As Range
x = 0

Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
For Each c In rng
ReDim Preserve arrExcelValues(x)
arrExcelValues(x) = c.Value
x = x + 1
Next
For Each strItem In arrExcelValues
MsgBox strItem
Next

End Sub

"don" wrote:

> I would like to build an array by looping thru a set of criteria.
> When finished use the array to do some calculations based on the
> length of the array.
>
> However when I try to build an array by concatentating the rows found,
> ie rownum= rownum & "," & d.row
> the result is "11 , 12"
> which the array command views as 1 item in the array when I want it to
> be 2 items, namely 11 and 12.
> the result of msgbox ubound(array(rownum)) is 0
> How do I concatenate rows found into an array.
>
> Thanks
>
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Nov 2008
First off, concatenation is the process of taking two or more pieces of text
and joining them together into a single piece of text, so that is not what
you would want to do in order to make an array. Secondly, you appear to be
trying to create an array of row numbers. I'm not sure that makes any sense
to do as you can easily find a first and last row number and know all the
row numbers between them... there is no need to make an array for that
purpose. Perhaps if you tell us what you ultimate goal is, maybe someone
here will be able to offer you an different, more efficient, method than you
seem to now have in mind.

--
Rick (MVP - Excel)


"don" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I would like to build an array by looping thru a set of criteria.
> When finished use the array to do some calculations based on the
> length of the array.
>
> However when I try to build an array by concatentating the rows found,
> ie rownum= rownum & "," & d.row
> the result is "11 , 12"
> which the array command views as 1 item in the array when I want it to
> be 2 items, namely 11 and 12.
> the result of msgbox ubound(array(rownum)) is 0
> How do I concatenate rows found into an array.
>
> Thanks
>


 
Reply With Quote
 
don
Guest
Posts: n/a
 
      17th Nov 2008
Thanks Mike.
that fit the bill.
did what I needed.


On Sun, 16 Nov 2008 18:23:05 -0800, Mike
<(E-Mail Removed)> wrote:

>This looks at Column A
>Sub loopArray()
> Dim arrExcelValues()
> Dim rng As Range
> x = 0
>
> Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> For Each c In rng
> ReDim Preserve arrExcelValues(x)
> arrExcelValues(x) = c.Value
> x = x + 1
> Next
> For Each strItem In arrExcelValues
> MsgBox strItem
> Next
>
>End Sub
>
>"don" wrote:
>
>> I would like to build an array by looping thru a set of criteria.
>> When finished use the array to do some calculations based on the
>> length of the array.
>>
>> However when I try to build an array by concatentating the rows found,
>> ie rownum= rownum & "," & d.row
>> the result is "11 , 12"
>> which the array command views as 1 item in the array when I want it to
>> be 2 items, namely 11 and 12.
>> the result of msgbox ubound(array(rownum)) is 0
>> How do I concatenate rows found into an array.
>>
>> Thanks
>>
>>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      17th Nov 2008

> For Each c In rng
> ReDim Preserve arrExcelValues(x)


ReDim Preserve is an expensive operation and should be avoided if at
all possible. Since you already know how many elements there will be
(rng.Cells.Count), you can use a single ReDim to size the array
appropriately. For example,


Sub Array2()
Dim arrExcelValues() As Variant
Dim rng As Range
x = 0
Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
ReDim arrexcevalues(1 To rng.Cells.Count)
For Each c In rng
x = x + 1
arrExcelValues(x) = c.Value
Next c

End Sub


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


On Sun, 16 Nov 2008 18:23:05 -0800, Mike
<(E-Mail Removed)> wrote:

>This looks at Column A
>Sub loopArray()
> Dim arrExcelValues()
> Dim rng As Range
> x = 0
>
> Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> For Each c In rng
> ReDim Preserve arrExcelValues(x)
> arrExcelValues(x) = c.Value
> x = x + 1
> Next
> For Each strItem In arrExcelValues
> MsgBox strItem
> Next
>
>End Sub
>
>"don" wrote:
>
>> I would like to build an array by looping thru a set of criteria.
>> When finished use the array to do some calculations based on the
>> length of the array.
>>
>> However when I try to build an array by concatentating the rows found,
>> ie rownum= rownum & "," & d.row
>> the result is "11 , 12"
>> which the array command views as 1 item in the array when I want it to
>> be 2 items, namely 11 and 12.
>> the result of msgbox ubound(array(rownum)) is 0
>> How do I concatenate rows found into an array.
>>
>> Thanks
>>
>>

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      17th Nov 2008
Chip
I get a script out of range error with your suggestion.

"Chip Pearson" wrote:

>
> > For Each c In rng
> > ReDim Preserve arrExcelValues(x)

>
> ReDim Preserve is an expensive operation and should be avoided if at
> all possible. Since you already know how many elements there will be
> (rng.Cells.Count), you can use a single ReDim to size the array
> appropriately. For example,
>
>
> Sub Array2()
> Dim arrExcelValues() As Variant
> Dim rng As Range
> x = 0
> Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> ReDim arrexcevalues(1 To rng.Cells.Count)
> For Each c In rng
> x = x + 1
> arrExcelValues(x) = c.Value
> Next c
>
> End Sub
>
>
> Cordially,
> Chip Pearson
> Microsoft MVP
> Excel Product Group
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
> On Sun, 16 Nov 2008 18:23:05 -0800, Mike
> <(E-Mail Removed)> wrote:
>
> >This looks at Column A
> >Sub loopArray()
> > Dim arrExcelValues()
> > Dim rng As Range
> > x = 0
> >
> > Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> > For Each c In rng
> > ReDim Preserve arrExcelValues(x)
> > arrExcelValues(x) = c.Value
> > x = x + 1
> > Next
> > For Each strItem In arrExcelValues
> > MsgBox strItem
> > Next
> >
> >End Sub
> >
> >"don" wrote:
> >
> >> I would like to build an array by looping thru a set of criteria.
> >> When finished use the array to do some calculations based on the
> >> length of the array.
> >>
> >> However when I try to build an array by concatentating the rows found,
> >> ie rownum= rownum & "," & d.row
> >> the result is "11 , 12"
> >> which the array command views as 1 item in the array when I want it to
> >> be 2 items, namely 11 and 12.
> >> the result of msgbox ubound(array(rownum)) is 0
> >> How do I concatenate rows found into an array.
> >>
> >> Thanks
> >>
> >>

>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      17th Nov 2008
>I get a script out of range error with your suggestion.

Shame on me for not testing the code, and shame on you for not using
Option Explicit.

> ReDim arrexcevalues(1 To rng.Cells.Count)

should be
> ReDim arrexcelvalues(1 To rng.Cells.Count)


The name of the array is misspelled.

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



On Mon, 17 Nov 2008 05:26:01 -0800, Mike
<(E-Mail Removed)> wrote:

>Chip
>I get a script out of range error with your suggestion.
>
>"Chip Pearson" wrote:
>
>>
>> > For Each c In rng
>> > ReDim Preserve arrExcelValues(x)

>>
>> ReDim Preserve is an expensive operation and should be avoided if at
>> all possible. Since you already know how many elements there will be
>> (rng.Cells.Count), you can use a single ReDim to size the array
>> appropriately. For example,
>>
>>
>> Sub Array2()
>> Dim arrExcelValues() As Variant
>> Dim rng As Range
>> x = 0
>> Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
>> ReDim arrexcevalues(1 To rng.Cells.Count)
>> For Each c In rng
>> x = x + 1
>> arrExcelValues(x) = c.Value
>> Next c
>>
>> End Sub
>>
>>
>> Cordially,
>> Chip Pearson
>> Microsoft MVP
>> Excel Product Group
>> Pearson Software Consulting, LLC
>> www.cpearson.com
>> (email on web site)
>>
>>
>> On Sun, 16 Nov 2008 18:23:05 -0800, Mike
>> <(E-Mail Removed)> wrote:
>>
>> >This looks at Column A
>> >Sub loopArray()
>> > Dim arrExcelValues()
>> > Dim rng As Range
>> > x = 0
>> >
>> > Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
>> > For Each c In rng
>> > ReDim Preserve arrExcelValues(x)
>> > arrExcelValues(x) = c.Value
>> > x = x + 1
>> > Next
>> > For Each strItem In arrExcelValues
>> > MsgBox strItem
>> > Next
>> >
>> >End Sub
>> >
>> >"don" wrote:
>> >
>> >> I would like to build an array by looping thru a set of criteria.
>> >> When finished use the array to do some calculations based on the
>> >> length of the array.
>> >>
>> >> However when I try to build an array by concatentating the rows found,
>> >> ie rownum= rownum & "," & d.row
>> >> the result is "11 , 12"
>> >> which the array command views as 1 item in the array when I want it to
>> >> be 2 items, namely 11 and 12.
>> >> the result of msgbox ubound(array(rownum)) is 0
>> >> How do I concatenate rows found into an array.
>> >>
>> >> Thanks
>> >>
>> >>

>>

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      17th Nov 2008
LOL... I saw that it was misspelled and changed it but it still didnt work. I
reopened the workbook and now it is working.

"Chip Pearson" wrote:

> >I get a script out of range error with your suggestion.

>
> Shame on me for not testing the code, and shame on you for not using
> Option Explicit.
>
> > ReDim arrexcevalues(1 To rng.Cells.Count)

> should be
> > ReDim arrexcelvalues(1 To rng.Cells.Count)

>
> The name of the array is misspelled.
>
> Cordially,
> Chip Pearson
> Microsoft MVP
> Excel Product Group
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
>
> On Mon, 17 Nov 2008 05:26:01 -0800, Mike
> <(E-Mail Removed)> wrote:
>
> >Chip
> >I get a script out of range error with your suggestion.
> >
> >"Chip Pearson" wrote:
> >
> >>
> >> > For Each c In rng
> >> > ReDim Preserve arrExcelValues(x)
> >>
> >> ReDim Preserve is an expensive operation and should be avoided if at
> >> all possible. Since you already know how many elements there will be
> >> (rng.Cells.Count), you can use a single ReDim to size the array
> >> appropriately. For example,
> >>
> >>
> >> Sub Array2()
> >> Dim arrExcelValues() As Variant
> >> Dim rng As Range
> >> x = 0
> >> Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> >> ReDim arrexcevalues(1 To rng.Cells.Count)
> >> For Each c In rng
> >> x = x + 1
> >> arrExcelValues(x) = c.Value
> >> Next c
> >>
> >> End Sub
> >>
> >>
> >> Cordially,
> >> Chip Pearson
> >> Microsoft MVP
> >> Excel Product Group
> >> Pearson Software Consulting, LLC
> >> www.cpearson.com
> >> (email on web site)
> >>
> >>
> >> On Sun, 16 Nov 2008 18:23:05 -0800, Mike
> >> <(E-Mail Removed)> wrote:
> >>
> >> >This looks at Column A
> >> >Sub loopArray()
> >> > Dim arrExcelValues()
> >> > Dim rng As Range
> >> > x = 0
> >> >
> >> > Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
> >> > For Each c In rng
> >> > ReDim Preserve arrExcelValues(x)
> >> > arrExcelValues(x) = c.Value
> >> > x = x + 1
> >> > Next
> >> > For Each strItem In arrExcelValues
> >> > MsgBox strItem
> >> > Next
> >> >
> >> >End Sub
> >> >
> >> >"don" wrote:
> >> >
> >> >> I would like to build an array by looping thru a set of criteria.
> >> >> When finished use the array to do some calculations based on the
> >> >> length of the array.
> >> >>
> >> >> However when I try to build an array by concatentating the rows found,
> >> >> ie rownum= rownum & "," & d.row
> >> >> the result is "11 , 12"
> >> >> which the array command views as 1 item in the array when I want it to
> >> >> be 2 items, namely 11 and 12.
> >> >> the result of msgbox ubound(array(rownum)) is 0
> >> >> How do I concatenate rows found into an array.
> >> >>
> >> >> Thanks
> >> >>
> >> >>
> >>

>

 
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
dynamically build an array Phillll Peeps via .NET 247 Microsoft Dot NET 1 10th Apr 2005 04:15 PM
build up an array using formulas =?Utf-8?B?S2V6emU=?= Microsoft Excel Programming 3 1st Mar 2005 04:57 PM
build up an array using formulas =?Utf-8?B?S2V6emU=?= Microsoft Excel Misc 0 15th Feb 2005 02:17 PM
I want to build a 2.8TB storage array Yeechang Lee DIY PC 21 12th Jan 2005 01:00 AM
Array Expansion Re Build ? serverguy Microsoft Windows 2000 Networking 3 14th Aug 2004 11:37 AM


Features
 

Advertising
 

Newsgroups
 


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