PC Review


Reply
Thread Tools Rate Thread

Is the array empty?

 
 
FortisChet@gmail.com
Guest
Posts: n/a
 
      14th Mar 2008
Hi

I am using a macro to load an array with data from excel. This is time
series data. As I loop through all the years and months, there are
some months that don't exist in a specific year (eg for 2005, the data
starts from April, whereas my array loops over all months).

So when I look up this array in the local variables, each element of
the array shows up as EMPTY.

this causes a problem cos at the end of all this, I'm trying to do :

worksheetfunction.average(array)

and it fails when I the array is empty.

Is there anyway to determine if the array is empty, cos then I can use
an if statement to circumvent.

I've tried using lbound, ubound, isempty etc etc...nothing seems to be
working.

Suggestions are deeply appreciated.

Thanks
Chet
 
Reply With Quote
 
 
 
 
JP
Guest
Posts: n/a
 
      14th Mar 2008
I found this on another board, does it help?

If IsError(Application.Match("*", (myArray), 0)) Then
MsgBox "Empty array"
Else
MsgBox "Something, somewhere"
End If

--JP


On Mar 14, 6:58*pm, "FortisC...@gmail.com" <FortisC...@gmail.com>
wrote:
> Hi
>
> I am using a macro to load an array with data from excel. This is time
> series data. As I loop through all the years and months, there are
> some months that don't exist in a specific year (eg for 2005, the data
> starts from April, whereas my array loops over all months).
>
> So when I look up this array in the local variables, each element of
> the array shows up as EMPTY.
>
> this causes a problem cos at the end of all this, I'm trying to do :
>
> worksheetfunction.average(array)
>
> and it fails when I the array is empty.
>
> Is there anyway to determine if the array is empty, cos then I can use
> an if statement to circumvent.
>
> I've tried using lbound, ubound, isempty etc etc...nothing seems to be
> working.
>
> Suggestions are deeply appreciated.
>
> Thanks
> Chet


 
Reply With Quote
 
FortisChet@gmail.com
Guest
Posts: n/a
 
      14th Mar 2008
On Mar 14, 6:02 pm, JP <jp2...@earthlink.net> wrote:
> I found this on another board, does it help?
>
> If IsError(Application.Match("*", (myArray), 0)) Then
> MsgBox "Empty array"
> Else
> MsgBox "Something, somewhere"
> End If
>
> --JP
>
> On Mar 14, 6:58 pm, "FortisC...@gmail.com" <FortisC...@gmail.com>
> wrote:
>
> > Hi

>
> > I am using a macro to load an array with data from excel. This is time
> > series data. As I loop through all the years and months, there are
> > some months that don't exist in a specific year (eg for 2005, the data
> > starts from April, whereas my array loops over all months).

>
> > So when I look up this array in the local variables, each element of
> > the array shows up as EMPTY.

>
> > this causes a problem cos at the end of all this, I'm trying to do :

>
> > worksheetfunction.average(array)

>
> > and it fails when I the array is empty.

>
> > Is there anyway to determine if the array is empty, cos then I can use
> > an if statement to circumvent.

>
> > I've tried using lbound, ubound, isempty etc etc...nothing seems to be
> > working.

>
> > Suggestions are deeply appreciated.

>
> > Thanks
> > Chet



WOW! Yeah it seems to be working, i'll run a few loops to test it. I
guess the " * " is the generic for any non empty element right? so it
tests if the array has ANYTHING non empty and then gives the
corresponding msg.

Thanks a ton for this one.

Truly
Chet



 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      14th Mar 2008
I use the following function in my standard code library. You pass in a
variable and it returns True if that variable is an allocated array. It
returns False if the variable is not an array, is an unallocated or Erase'd
array, or is an array whose LBound > UBound.

Function IsArrayAllocated(V As Variant) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsArrayAllocated
' Returns True if V is a static array or an allocated
' dynamic array. Returns False if V is:
' - not an array, or
' - an Erase'd or unallocated dynamic array, or
' - an array whose LBound is > UBound (e.g., failure
' of Split).
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
IsArrayAllocated = IsArray(V) And _
Not IsError(LBound(V)) And _
(LBound(V) <= UBound(V))
End Function

You can use it in code like

Dim V As Variant ' or V() As whatever
' do something with V
If IsArrayAllocated(V) = True Then
Debug.Print "V is an allocated array"
Else
Debug.Print "V is not an allocated array"
End If


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




<(E-Mail Removed)> wrote in message
news:70359399-c6de-4685-848c-(E-Mail Removed)...
> Hi
>
> I am using a macro to load an array with data from excel. This is time
> series data. As I loop through all the years and months, there are
> some months that don't exist in a specific year (eg for 2005, the data
> starts from April, whereas my array loops over all months).
>
> So when I look up this array in the local variables, each element of
> the array shows up as EMPTY.
>
> this causes a problem cos at the end of all this, I'm trying to do :
>
> worksheetfunction.average(array)
>
> and it fails when I the array is empty.
>
> Is there anyway to determine if the array is empty, cos then I can use
> an if statement to circumvent.
>
> I've tried using lbound, ubound, isempty etc etc...nothing seems to be
> working.
>
> Suggestions are deeply appreciated.
>
> Thanks
> Chet


 
Reply With Quote
 
Matthew Pfluger
Guest
Posts: n/a
 
      7th May 2008
Excellent function, Chip. Thanks.

Matthew Pfluger

"Chip Pearson" wrote:

> I use the following function in my standard code library. You pass in a
> variable and it returns True if that variable is an allocated array. It
> returns False if the variable is not an array, is an unallocated or Erase'd
> array, or is an array whose LBound > UBound.
>
> Function IsArrayAllocated(V As Variant) As Boolean
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' IsArrayAllocated
> ' Returns True if V is a static array or an allocated
> ' dynamic array. Returns False if V is:
> ' - not an array, or
> ' - an Erase'd or unallocated dynamic array, or
> ' - an array whose LBound is > UBound (e.g., failure
> ' of Split).
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> On Error Resume Next
> IsArrayAllocated = IsArray(V) And _
> Not IsError(LBound(V)) And _
> (LBound(V) <= UBound(V))
> End Function
>
> You can use it in code like
>
> Dim V As Variant ' or V() As whatever
> ' do something with V
> If IsArrayAllocated(V) = True Then
> Debug.Print "V is an allocated array"
> Else
> Debug.Print "V is not an allocated array"
> End If
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional
> Excel Product Group
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
>
>
> <(E-Mail Removed)> wrote in message
> news:70359399-c6de-4685-848c-(E-Mail Removed)...
> > Hi
> >
> > I am using a macro to load an array with data from excel. This is time
> > series data. As I loop through all the years and months, there are
> > some months that don't exist in a specific year (eg for 2005, the data
> > starts from April, whereas my array loops over all months).
> >
> > So when I look up this array in the local variables, each element of
> > the array shows up as EMPTY.
> >
> > this causes a problem cos at the end of all this, I'm trying to do :
> >
> > worksheetfunction.average(array)
> >
> > and it fails when I the array is empty.
> >
> > Is there anyway to determine if the array is empty, cos then I can use
> > an if statement to circumvent.
> >
> > I've tried using lbound, ubound, isempty etc etc...nothing seems to be
> > working.
> >
> > Suggestions are deeply appreciated.
> >
> > Thanks
> > Chet

>

 
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
Empty an Array =?Utf-8?B?QnJhZA==?= Microsoft Access VBA Modules 3 5th Feb 2006 01:07 PM
Empty Array ExcelMonkey Microsoft Excel Programming 4 1st Mar 2005 04:10 PM
Array Empty After Sub Called ExcelMonkey Microsoft Excel Programming 2 1st Mar 2005 09:21 AM
Is the array empty? Otto Moehrbach Microsoft Excel Programming 4 13th Jun 2004 03:35 AM
Re: Array empty? Lloyd Sheen Microsoft Dot NET 0 28th Jan 2004 08:52 PM


Features
 

Advertising
 

Newsgroups
 


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