Consulting

Results 1 to 5 of 5

Thread: How to Clear an Array

  1. #1

    How to Clear an Array

    Is there a simple way in VBA to clear an array of all values?

  2. #2
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Redim with no preserve keyword?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Erase myArray

    It not only removes values, if it is a dynamic array, it releases it which means that it has no bounds.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Quote Originally Posted by xld
    Erase myArray

    It not only removes values, if it is a dynamic array, it releases it which means that it has no bounds.
    Does that mean I would have to ReDim it? What are the consequences of the erase? Can I still use it?

    Reset. I looked it up in Help. Thanx.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Cyberdude
    Does that mean I would have to ReDim it? What are the consequences of the erase? Can I still use it?

    Reset. I looked it up in Help. Thanx.
    The impact depends on what type of array is being erased. VBA help does a pretty good job explaining what happens with its example. You can still use "fixed" arrays as before. You will need to redim dynamic arrays to reallocate space.

    [from VBA Help]
    Erase Statement Example

    This example uses the Erase statement to reinitialize the elements of fixed-size arrays and deallocate dynamic-array storage space.

    ' Declare array variables.
    Dim NumArray(10) As Integer    ' Integer array.
    Dim StrVarArray(10) As String    ' Variable-string array.
    Dim StrFixArray(10) As String * 10    ' Fixed-string array.
    Dim VarArray(10) As Variant    ' Variant array.
    Dim DynamicArray() As Integer    ' Dynamic array.
    ReDim DynamicArray(10)    ' Allocate storage space.
    Erase NumArray    ' Each element set to 0.
    Erase StrVarArray    ' Each element set to zero-length 
        ' string ("").    
    Erase StrFixArray    ' Each element set to 0.
    Erase VarArray    ' Each element set to Empty.
    Erase DynamicArray    ' Free memory used by array.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

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