+ Reply to Thread
Results 1 to 7 of 7

Built-in-Function checking Array of Zero Length

  1. #1
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Built-in-Function checking Array of Zero Length

    Anyone knows if there is a built-in-function in VBA checking if an array has zero length or not.

    I have tried:
    * LBound - Creates an error if the array is zero length which I want to avoid
    * IsArray() - Always TRUE indpendent of zero length or not
    * InNull() - Always FALSE indpendent of zero length or not

    I have written a function bQuery_ArrayZeroLength() using LBound() with error handling which works. Code pasted below. I just want to know if there is a built-in-function for this.

    Run Sub MyTest() below to execute code.


    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Built-in-Function checking Array of Zero Length

    I don't know of an inbuilt function for this, I normally use something connected with the population of the array.

    For example if the array is dynamic and we are resizing it we would use a variable for the dimension, if that variable is 0 after the code that's mean to populate the array then you know the array is empty.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,994

    Re: Built-in-Function checking Array of Zero Length

    Use IsArray.

    http://support.microsoft.com/kb/151561


    Regards,TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Built-in-Function checking Array of Zero Length

    Simple test of Lbound or Ubound is not sufficient. For example if you will use Filter function on array that does not contain the item for which you filter, array will be returned with Lbound 0 and Ubound -1. It is better to test if Ubound (or LBound) returns error (if so, array is empty) then if no error, test if Lbound is lower than UBound - if not, array is also empty.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  5. #5
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Built-in-Function checking Array of Zero Length

    OK.

    I have created a function that returns a dynamic array. The returned array can be of zero length. I can add an argument in this function using ByRef that returns the size of the array.

    Quote Originally Posted by Norie View Post
    I don't know of an inbuilt function for this, I normally use something connected with the population of the array.

    For example if the array is dynamic and we are resizing it we would use a variable for the dimension, if that variable is 0 after the code that's mean to populate the array then you know the array is empty.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,994

    Re: Built-in-Function checking Array of Zero Length

    In your test, you define avTest using "Dim avTest() As Variant" which is a zero length Variant Array. That is, it is an array ... because of the brackets ... but, until you put something in it, it is empty.

    Regards, TMS

  7. #7
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Built-in-Function checking Array of Zero Length

    Something like this might work checking if a dynamic array is sized or not.

    Please Login or Register  to view this content.
    Quote Originally Posted by Izandol View Post
    Simple test of Lbound or Ubound is not sufficient. For example if you will use Filter function on array that does not contain the item for which you filter, array will be returned with Lbound 0 and Ubound -1. It is better to test if Ubound (or LBound) returns error (if so, array is empty) then if no error, test if Lbound is lower than UBound - if not, array is also empty.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Disabling built-in certificate checking
    By scottyyyc in forum Excel General
    Replies: 0
    Last Post: 01-11-2012, 10:42 AM
  2. Checking Character Length in a specific row and column
    By crisshinn in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-27-2011, 07:02 AM
  3. How do I pass a built array back to formula
    By sauerj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2009, 01:58 PM
  4. [SOLVED] Modify array function length
    By Fabian Grodek in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-08-2006, 11:40 AM
  5. Checking the length of cell contents
    By RichardG in forum Excel General
    Replies: 8
    Last Post: 02-21-2006, 08:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1