Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to determine an array is empty in VBA

How to determine an array is empty in VBA

Scheduled Pinned Locked Moved Visual Basic
databasedata-structureshelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmhu
    wrote on last edited by
    #1

    I'm writting VBA macro code with array start index as 1 rather 0 Option Base 1 Dim array() As String If()Then ReDim array(5) 'fill array with elements Else ReDim array(0) 'This statement gives error End If I try to use UBound() and LBound() to determine if the array is empty, but can't make it work. Someone please tell me if there is other way to make it work.

    D C 2 Replies Last reply
    0
    • M mmhu

      I'm writting VBA macro code with array start index as 1 rather 0 Option Base 1 Dim array() As String If()Then ReDim array(5) 'fill array with elements Else ReDim array(0) 'This statement gives error End If I try to use UBound() and LBound() to determine if the array is empty, but can't make it work. Someone please tell me if there is other way to make it work.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't ReDim an array down to 0 when the Option Base is 1. The lowest you can ReDim down to, obviously, is 1. I don't know what you mean by "if the array is empty". It appears as though you're trying to ReDim a variable that was an array to no longer be an array. You can't do that. Once a variable is declared, you cannot change it's type. If it's declared as an Integer or Long, it has to stay an Integer or Long. You can't ReDim it to make it an array of Longs. The opposite is also true. Once delcared as an array, it must stay as an array. You can't ReDim it down to a single-valued variable. I think the function you're looking for is IsArray(_var_). This will return True or False depending on if the _var_ is an array or not. **Dave Kreskowiak _Microsoft MVP - Visual Basic_**

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You can't ReDim an array down to 0 when the Option Base is 1. The lowest you can ReDim down to, obviously, is 1. I don't know what you mean by "if the array is empty". It appears as though you're trying to ReDim a variable that was an array to no longer be an array. You can't do that. Once a variable is declared, you cannot change it's type. If it's declared as an Integer or Long, it has to stay an Integer or Long. You can't ReDim it to make it an array of Longs. The opposite is also true. Once delcared as an array, it must stay as an array. You can't ReDim it down to a single-valued variable. I think the function you're looking for is IsArray(_var_). This will return True or False depending on if the _var_ is an array or not. **Dave Kreskowiak _Microsoft MVP - Visual Basic_**

        M Offline
        M Offline
        mmhu
        wrote on last edited by
        #3

        Hi, Dave, I declared an array, then under certain conditions, this array will be filled with elements, otherwise it will stay empty. After get through the condition statements, I need to determine if the array is empty. I tried to use UBound() and LBound(), but I receive compile error on it when the array is not ReDim and filled with elements. what is the best way to determine if the array is empty? Thanks Vicky

        D 1 Reply Last reply
        0
        • M mmhu

          Hi, Dave, I declared an array, then under certain conditions, this array will be filled with elements, otherwise it will stay empty. After get through the condition statements, I need to determine if the array is empty. I tried to use UBound() and LBound(), but I receive compile error on it when the array is not ReDim and filled with elements. what is the best way to determine if the array is empty? Thanks Vicky

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          "Empty" can be different things. If the variable is not declared an array, or if the variable is declared an array but has no elements, and it can be declared an array and have empty elements. It depends on the call that's creating the array. You just have to test it to find out. IsArray will tell you if the variable is declared as an array, but that's it. If that test passes, then you can check its UBound. If it's greater than 0, the array has elements. After that, you check the value of the first element to see if that value is valid.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          1 Reply Last reply
          0
          • M mmhu

            I'm writting VBA macro code with array start index as 1 rather 0 Option Base 1 Dim array() As String If()Then ReDim array(5) 'fill array with elements Else ReDim array(0) 'This statement gives error End If I try to use UBound() and LBound() to determine if the array is empty, but can't make it work. Someone please tell me if there is other way to make it work.

            C Offline
            C Offline
            Coding C
            wrote on last edited by
            #5

            Hi, try using this if ubound(array)>=1 then _'array has elements_ else _'array is empty_ endif hope this Works... Nitin...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups