PC Review


Reply
Thread Tools Rate Thread

Determining the size of an input array

 
 
Tom Kreutz
Guest
Posts: n/a
 
      5th Mar 2007
Dear Folks,

In Visual Basic, how does one determine the size of an array (range?)
being passed in from Excel? For example, if I wanted to write my own
version of SUM(), how would I know the size/length of the range that the
user has chosen?

Many thanks,

Tom Kreutz
 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      5th Mar 2007
One way:

Public Function foo(ByRef bar As Excel.Range) As Variant
foo = "Rows: " & bar.Rows.Count & vbNewLine & _
"Columns: " & bar.Columns.Count
End Function


In article <(E-Mail Removed)>,
Tom Kreutz <(E-Mail Removed)> wrote:

> Dear Folks,
>
> In Visual Basic, how does one determine the size of an array (range?)
> being passed in from Excel? For example, if I wanted to write my own
> version of SUM(), how would I know the size/length of the range that the
> user has chosen?
>
> Many thanks,
>
> Tom Kreutz

 
Reply With Quote
 
Tom Kreutz
Guest
Posts: n/a
 
      5th Mar 2007
Dear J.E.,

May your offspring and the all the successive generations that follow
be richly blessed!

(Oh yeah, and thanks a TON!)

Tom


JE McGimpsey wrote:
> One way:
>
> Public Function foo(ByRef bar As Excel.Range) As Variant
> foo = "Rows: " & bar.Rows.Count & vbNewLine & _
> "Columns: " & bar.Columns.Count
> End Function
>
>
> In article <(E-Mail Removed)>,
> Tom Kreutz <(E-Mail Removed)> wrote:
>
>> Dear Folks,
>>
>> In Visual Basic, how does one determine the size of an array (range?)
>> being passed in from Excel? For example, if I wanted to write my own
>> version of SUM(), how would I know the size/length of the range that the
>> user has chosen?
>>
>> Many thanks,
>>
>> Tom Kreutz

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      5th Mar 2007
I'm not sure what you're doing, but you could loop through each cell in the
range that was passed, too:

Option Explicit
Function mySum(rng as range) as double
dim myCell as range
dim myTotal as double

mytotal = 0

for each mycell in rng.cells
if isnumeric(mycell.value) then
mytotal = mytotal + mycell.value
end if
next mycell

mySum = myTotal

end function

(with not much error checking)



Tom Kreutz wrote:
>
> Dear Folks,
>
> In Visual Basic, how does one determine the size of an array (range?)
> being passed in from Excel? For example, if I wanted to write my own
> version of SUM(), how would I know the size/length of the range that the
> user has chosen?
>
> Many thanks,
>
> Tom Kreutz


--

Dave Peterson
 
Reply With Quote
 
Tom Kreutz
Guest
Posts: n/a
 
      6th Mar 2007
Dear Dave,

I'm attempting to load the values of a range (whose length is not
pre-set in the VB procedure) into a Double array within the procedure,
and then manipulate it. I was struggling to guess the syntax that would
allow me to step through the range, cell by cell, so that I could stuff
the values into the VB array. Your example was VERY helpful! (But I
must admit that the logic behind "for each mycell in rng.cells" is
straining my peewee noggin!)

Many thanks for your help!

Tom


Dave Peterson wrote:
> I'm not sure what you're doing, but you could loop through each cell in the
> range that was passed, too:
>
> Option Explicit
> Function mySum(rng as range) as double
> dim myCell as range
> dim myTotal as double
>
> mytotal = 0
>
> for each mycell in rng.cells
> if isnumeric(mycell.value) then
> mytotal = mytotal + mycell.value
> end if
> next mycell
>
> mySum = myTotal
>
> end function
>
> (with not much error checking)
>
>
>
> Tom Kreutz wrote:
>> Dear Folks,
>>
>> In Visual Basic, how does one determine the size of an array (range?)
>> being passed in from Excel? For example, if I wanted to write my own
>> version of SUM(), how would I know the size/length of the range that the
>> user has chosen?
>>
>> Many thanks,
>>
>> Tom Kreutz

>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      6th Mar 2007
You can actually pick up all the values in a single area range and plop them
into an array in one line:

dim myArr as variant
dim LastRow as long
with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
myarr = .range("a1:X" & lastrow).value
end with

I used column A to find the last row and went from A1:X(Lastrow).

You'll end up with a two dimensional array (rows by columns)--even if you pickup
a single column.

A1:A10 would be placed into a array 10 rows by 1 column.

But you could loop through each cell in the range.

dim iRow as long
dim iCol as long
dim LastRow as long
dim myrng as range
dim myCell as range
dim myRow as range
dim myArr() as variant

with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
set myrng = .range("a1:X" & lastrow)
end with

redim myArr(myrng.rows.count, myrng.columns.count)
for irow = 1 to myrng.rows.count
for icol = 1 to myrng.columns.count
myarr(irow,icol) = myrng(irow,icol).value
next icol
next irow





Tom Kreutz wrote:
>
> Dear Dave,
>
> I'm attempting to load the values of a range (whose length is not
> pre-set in the VB procedure) into a Double array within the procedure,
> and then manipulate it. I was struggling to guess the syntax that would
> allow me to step through the range, cell by cell, so that I could stuff
> the values into the VB array. Your example was VERY helpful! (But I
> must admit that the logic behind "for each mycell in rng.cells" is
> straining my peewee noggin!)
>
> Many thanks for your help!
>
> Tom
>
> Dave Peterson wrote:
> > I'm not sure what you're doing, but you could loop through each cell in the
> > range that was passed, too:
> >
> > Option Explicit
> > Function mySum(rng as range) as double
> > dim myCell as range
> > dim myTotal as double
> >
> > mytotal = 0
> >
> > for each mycell in rng.cells
> > if isnumeric(mycell.value) then
> > mytotal = mytotal + mycell.value
> > end if
> > next mycell
> >
> > mySum = myTotal
> >
> > end function
> >
> > (with not much error checking)
> >
> >
> >
> > Tom Kreutz wrote:
> >> Dear Folks,
> >>
> >> In Visual Basic, how does one determine the size of an array (range?)
> >> being passed in from Excel? For example, if I wanted to write my own
> >> version of SUM(), how would I know the size/length of the range that the
> >> user has chosen?
> >>
> >> Many thanks,
> >>
> >> Tom Kreutz

> >


--

Dave Peterson
 
Reply With Quote
 
Tom Kreutz
Guest
Posts: n/a
 
      6th Mar 2007
Dear Dave,

Thanks again for your very helpful comments.

The code in your latter note seems to be tied to a range whose
position on the sheet is known or fixed. I'm interested in a routine
that, like SUM(), will process an arbitrary range. The first step in
the procedure is to extract the values in the (one dimensional) input
range and stuff them into a VB array. To that end, your first email
seems more relevant.

Thanks again,

Tom


Dave Peterson wrote:
> You can actually pick up all the values in a single area range and plop them
> into an array in one line:
>
> dim myArr as variant
> dim LastRow as long
> with activesheet
> lastrow = .cells(.rows.count,"A").end(xlup).row
> myarr = .range("a1:X" & lastrow).value
> end with
>
> I used column A to find the last row and went from A1:X(Lastrow).
>
> You'll end up with a two dimensional array (rows by columns)--even if you pickup
> a single column.
>
> A1:A10 would be placed into a array 10 rows by 1 column.
>
> But you could loop through each cell in the range.
>
> dim iRow as long
> dim iCol as long
> dim LastRow as long
> dim myrng as range
> dim myCell as range
> dim myRow as range
> dim myArr() as variant
>
> with activesheet
> lastrow = .cells(.rows.count,"A").end(xlup).row
> set myrng = .range("a1:X" & lastrow)
> end with
>
> redim myArr(myrng.rows.count, myrng.columns.count)
> for irow = 1 to myrng.rows.count
> for icol = 1 to myrng.columns.count
> myarr(irow,icol) = myrng(irow,icol).value
> next icol
> next irow
>
>
>
>
>
> Tom Kreutz wrote:
>> Dear Dave,
>>
>> I'm attempting to load the values of a range (whose length is not
>> pre-set in the VB procedure) into a Double array within the procedure,
>> and then manipulate it. I was struggling to guess the syntax that would
>> allow me to step through the range, cell by cell, so that I could stuff
>> the values into the VB array. Your example was VERY helpful! (But I
>> must admit that the logic behind "for each mycell in rng.cells" is
>> straining my peewee noggin!)
>>
>> Many thanks for your help!
>>
>> Tom
>>
>> Dave Peterson wrote:
>>> I'm not sure what you're doing, but you could loop through each cell in the
>>> range that was passed, too:
>>>
>>> Option Explicit
>>> Function mySum(rng as range) as double
>>> dim myCell as range
>>> dim myTotal as double
>>>
>>> mytotal = 0
>>>
>>> for each mycell in rng.cells
>>> if isnumeric(mycell.value) then
>>> mytotal = mytotal + mycell.value
>>> end if
>>> next mycell
>>>
>>> mySum = myTotal
>>>
>>> end function
>>>
>>> (with not much error checking)
>>>
>>>
>>>
>>> Tom Kreutz wrote:
>>>> Dear Folks,
>>>>
>>>> In Visual Basic, how does one determine the size of an array (range?)
>>>> being passed in from Excel? For example, if I wanted to write my own
>>>> version of SUM(), how would I know the size/length of the range that the
>>>> user has chosen?
>>>>
>>>> Many thanks,
>>>>
>>>> Tom Kreutz

>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      6th Mar 2007
The range may be of arbitrary size, but why can't you determine what it is?

Can you base it on the current selection? The activecell's current region? The
usedrange of that worksheet?

Maybe you can give some more info.

Tom Kreutz wrote:
>
> Dear Dave,
>
> Thanks again for your very helpful comments.
>
> The code in your latter note seems to be tied to a range whose
> position on the sheet is known or fixed. I'm interested in a routine
> that, like SUM(), will process an arbitrary range. The first step in
> the procedure is to extract the values in the (one dimensional) input
> range and stuff them into a VB array. To that end, your first email
> seems more relevant.
>
> Thanks again,
>
> Tom
>
> Dave Peterson wrote:
> > You can actually pick up all the values in a single area range and plop them
> > into an array in one line:
> >
> > dim myArr as variant
> > dim LastRow as long
> > with activesheet
> > lastrow = .cells(.rows.count,"A").end(xlup).row
> > myarr = .range("a1:X" & lastrow).value
> > end with
> >
> > I used column A to find the last row and went from A1:X(Lastrow).
> >
> > You'll end up with a two dimensional array (rows by columns)--even if you pickup
> > a single column.
> >
> > A1:A10 would be placed into a array 10 rows by 1 column.
> >
> > But you could loop through each cell in the range.
> >
> > dim iRow as long
> > dim iCol as long
> > dim LastRow as long
> > dim myrng as range
> > dim myCell as range
> > dim myRow as range
> > dim myArr() as variant
> >
> > with activesheet
> > lastrow = .cells(.rows.count,"A").end(xlup).row
> > set myrng = .range("a1:X" & lastrow)
> > end with
> >
> > redim myArr(myrng.rows.count, myrng.columns.count)
> > for irow = 1 to myrng.rows.count
> > for icol = 1 to myrng.columns.count
> > myarr(irow,icol) = myrng(irow,icol).value
> > next icol
> > next irow
> >
> >
> >
> >
> >
> > Tom Kreutz wrote:
> >> Dear Dave,
> >>
> >> I'm attempting to load the values of a range (whose length is not
> >> pre-set in the VB procedure) into a Double array within the procedure,
> >> and then manipulate it. I was struggling to guess the syntax that would
> >> allow me to step through the range, cell by cell, so that I could stuff
> >> the values into the VB array. Your example was VERY helpful! (But I
> >> must admit that the logic behind "for each mycell in rng.cells" is
> >> straining my peewee noggin!)
> >>
> >> Many thanks for your help!
> >>
> >> Tom
> >>
> >> Dave Peterson wrote:
> >>> I'm not sure what you're doing, but you could loop through each cell in the
> >>> range that was passed, too:
> >>>
> >>> Option Explicit
> >>> Function mySum(rng as range) as double
> >>> dim myCell as range
> >>> dim myTotal as double
> >>>
> >>> mytotal = 0
> >>>
> >>> for each mycell in rng.cells
> >>> if isnumeric(mycell.value) then
> >>> mytotal = mytotal + mycell.value
> >>> end if
> >>> next mycell
> >>>
> >>> mySum = myTotal
> >>>
> >>> end function
> >>>
> >>> (with not much error checking)
> >>>
> >>>
> >>>
> >>> Tom Kreutz wrote:
> >>>> Dear Folks,
> >>>>
> >>>> In Visual Basic, how does one determine the size of an array (range?)
> >>>> being passed in from Excel? For example, if I wanted to write my own
> >>>> version of SUM(), how would I know the size/length of the range that the
> >>>> user has chosen?
> >>>>
> >>>> Many thanks,
> >>>>
> >>>> Tom Kreutz

> >


--

Dave Peterson
 
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
Determining where keystroke input comes from Jim Franklin Microsoft Access 1 1st Dec 2008 02:02 PM
Determining where keystroke input comes from Jim Franklin Microsoft Access Form Coding 1 1st Dec 2008 02:02 PM
determining array with no bounds set? =?Utf-8?B?bWFyaw==?= Microsoft Excel Programming 1 22nd Jun 2007 09:53 PM
Determining Array Limit =?Utf-8?B?Q2hhcGxhaW4gRG91Zw==?= Microsoft Excel Programming 3 5th Jan 2005 01:37 AM
Determining which control has input focus? Dave Veeneman Microsoft C# .NET 3 13th Nov 2003 09:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:57 AM.