Clearing an Array

  • Thread starter Thread starter Cloudfall
  • Start date Start date
C

Cloudfall

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.
 
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.
 
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
 
Take a look at Erase in VBA's help.


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.
 
Robert,

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

Regards,

Terry.
 
Back
Top