Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    How do I test for an empty array?

    Hello loungers

    MS Word VBA

    Is there a recommended way of testing whether an array is empty?

    I have a dynamic array which I later redim with redim Preserve
    dim arName()
    .
    redim preserve arName(variable)


    By empty I mean that no data at all has been written into it. The redim preserve has never been executed.

    Regards and thanks
    Geof

  2. #2
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,629
    Thanks
    114
    Thanked 644 Times in 588 Posts
    Geof,

    Adapt the following code. Toggle off and on populating the array values by commenting and uncommenting the array statement.

    Code:
    Public Sub dfgdfg()
    Dim num As Variant
    'num = Array(1, 2, 3, 4, 5)
    If IsEmpty(num) Then
        MsgBox "Empty"
    Else:
        MsgBox "Not Empty"
    End If
    End Sub
    HTH,
    Maud

  3. The Following User Says Thank You to Maudibe For This Useful Post:

    geofrichardson (2015-07-14)

  4. #3
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    Red face

    Hi Maud
    Thanks a lot.
    Solved in seconds just for asking. I had spent a number of hours searching in all the wrong places.
    I declared a new variable 'aData'
    The solution became
    Code:
            If IsEmpty(aData) Then
                    MsgBox ("Array empty")
                    Exit Sub
    .
    .
    .later lines
                        Hl = Hl + 1
                        ReDim Preserve oHighlights(Hl)
                        oHighlights(Hl) = aRange.Text
                        aData = Array(oHighlights) ' your help went here
    Till now I had been triggering error code 9 and trapping for it. I knew it was ugly but didnt know how to make it elegant.
    Brilliant help.
    Geof

Posting Permissions

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