Solved

redim preserve an array to a smaller amount

Posted on 2002-03-21
6
677 Views
Last Modified: 2012-06-27
Hi

Im rediming an array to a smaller amount and Im getting an error 'subscript out of range'

eg
dim arr()

'get y
redim preserve arr(y,8)

' catch that arr should be smaller so
''get  x
redim preserve arr(x,8) - this is where I get the error

any ideas??
0
Comment
Question by:louise_8
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Clif
Comment Utility
This is a limitation of VB.

According to the help file (for "ReDim Statement"):
"If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array."
0
 
LVL 6

Expert Comment

by:ebosscher
Comment Utility
Clif's got it
0
 
LVL 18

Accepted Solution

by:
mdougan earned 20 total points
Comment Utility
Yes, what Clif said is correct.  In your example, you can probably get away with switching the position of the dimensions:

dim arr()

'get y
redim preserve arr(8,y)

' catch that arr should be smaller so
''get  x
redim preserve arr(8,x)

Keep in mind that while the available array elements would be less, you are not actually freeing up any memory by doing this.  The only way memory would get freed for this array is if you :

Dim a temp array the size of your perm array
copy perm array to a temp array
Erase perm array
redim perm array
copy temp array to perm array
erase temp array
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:Clif
Comment Utility
If "Clif's got it" and "what Clif said is correct" then why did the grade and points go to mdougan?
0
 
LVL 6

Expert Comment

by:ebosscher
Comment Utility
Because mdougan suggested a work around
0
 
LVL 10

Expert Comment

by:Clif
Comment Utility
Oh.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now