Written by Allen Wyatt (last updated January 31, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
If you have done any programming in VBA, you know the value of using variable arrays to store information. It is not uncommon to start working with large arrays in your macros. For instance, you might declare a 100-element string array, as follows:
Dim MyText(99) As String
As your macro executes, information can be stored and restored in the elements of the array. At some point, you may want to erase all the information in the array. One classic way of doing this is using a For ... Next loop to step through each array element, as follows:
For J = 0 To 99 MyText(J) = "" Next J
When the looping is complete, everything has been erased from the array. A quicker way of accomplishing the same task is to use the Erase function, as follows:
Erase MyText
Once executed, this single line sets each element of the MyText array back to an empty string. If the array is numeric, then each element of the array is set to zero.
There is one caveat with using the Erase function: If the array being erased were originally dimensioned at run time using the ReDim statement, then Erase gets rid of the dimensions and you'll need to use ReDim again to set them.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9393) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Quickly Dumping Array Contents.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...
Discover MoreIf you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...
Discover MoreIf you have a large, complex workbook, you may want to make sure that it is always calculated manually instead of ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments