VBA empties array elements and dynamically allocates memory

Source: Internet
Author: User

Sample code
1, for the static array demo

The code is as follows Copy Code
Sub Test1 ()
Dim ARR (0 to 9) as Integer
For i = 0 to 9
ARR (i) = i
Next
For i = 0 to 9
Debug.Print Arr (i)
Next
' Above will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 in Immediate window
Erase ARR
For i = 0 to 9
Debug.Print Arr (i)
Next
' Above will output 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 in Immediate window
End Sub

You can see that the erase statement cleans up the values of the static array.


VBA Cleanup Array

2, for the dynamic array demo

The code is as follows Copy Code
Sub Test2 ()
Dim ARR () as Integer
ReDim ARR (9)
Debug.Print UBound (ARR)
Debug. Print Erase (ARR)
Debug.Print UBound (ARR) ' here will be an error
' Because the dynamically allocated memory has been released
End Sub

You can see that the erase statement frees up the memory we allocate to the dynamic array.

Original from: http://iofai.com/1305.html thank stationmaster

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.