• How do I test for an empty array?

    Author
    Topic
    #500932

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1515167

      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
      [COLOR=”#008000″]’num = Array(1, 2, 3, 4, 5)[/COLOR]
      If IsEmpty(num) Then
          MsgBox “Empty”
      Else:
          MsgBox “Not Empty”
      End If
      End Sub
      

      HTH,
      Maud

      • #1515170

        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
                            [B]aData = Array(oHighlights)[/B] ' 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

    Viewing 0 reply threads
    Reply To: How do I test for an empty array?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: