+ Reply to Thread
Results 1 to 6 of 6

Clearing an Array

Hybrid View

  1. #1
    Cloudfall
    Guest

    Clearing an Array

    Hi everyone,

    I want to clear the contents of an array. I know that redimensioning an
    array also clears it. So, I decided to clear the array by
    redimensioning to zero and then re-redimensioning it back to what it
    was. So, I wrote the following:

    Sub test()
    Static vDlt(1 To 52) As Boolean

    ReDim vDlt(0) As Boolean
    ReDim vDlt(1 To 52) As Boolean
    End Sub

    When I run this I get the error message "Compile error: array already
    dimensioned". Microsoft help has this to say on the issue:

    "You can use the ReDim statement repeatedly to change the number of
    elements and dimensions in an array."

    Does anybody know what is going on here? Also, is there a more elegant
    way of clearing the array?

    Thank you now for any responses but I will always get back to you with
    feedback.

    Regards,

    Terry.


  2. #2
    Cloudfall
    Guest

    Re: Clearing an Array

    Hi,

    I figured out why I was getting the error message (I didn't declare the
    array without dimension subscripts). But I still want to clear the
    array elegantly. Anyone?

    Regards,

    T.


  3. #3
    Robert
    Guest

    Re: Clearing an Array

    You need to dimension the array as a dynamic array vDlt() then you can
    use redim to clear the array

    eg:

    Sub test()
    Static vDlt() As Boolean
    ReDim vDlt(1 To 52) As Boolean

    ReDim vDlt(0) As Boolean
    ReDim vDlt(1 To 52) As Boolean
    End Sub


  4. #4
    Cloudfall
    Guest

    Re: Clearing an Array

    Robert,

    Thank you for your response. Posting questions about redimensioning
    arrays and receiving replies has furthered my understanding of this
    issue.

    Regards,

    Terry.


  5. #5
    Dave Peterson
    Guest

    Re: Clearing an Array

    Take a look at Erase in VBA's help.



    Cloudfall wrote:
    >
    > Hi everyone,
    >
    > I want to clear the contents of an array. I know that redimensioning an
    > array also clears it. So, I decided to clear the array by
    > redimensioning to zero and then re-redimensioning it back to what it
    > was. So, I wrote the following:
    >
    > Sub test()
    > Static vDlt(1 To 52) As Boolean
    >
    > ReDim vDlt(0) As Boolean
    > ReDim vDlt(1 To 52) As Boolean
    > End Sub
    >
    > When I run this I get the error message "Compile error: array already
    > dimensioned". Microsoft help has this to say on the issue:
    >
    > "You can use the ReDim statement repeatedly to change the number of
    > elements and dimensions in an array."
    >
    > Does anybody know what is going on here? Also, is there a more elegant
    > way of clearing the array?
    >
    > Thank you now for any responses but I will always get back to you with
    > feedback.
    >
    > Regards,
    >
    > Terry.


    --

    Dave Peterson

  6. #6
    Cloudfall
    Guest

    Re: Clearing an Array

    Thank you very much for this suggestion.

    Regards,

    Terry.


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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