Solved

redim preserve an array to a smaller amount

Posted on 2002-03-21
6
808 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 6886244
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
ID: 6886813
Clif's got it
0
 
LVL 18

Accepted Solution

by:
mdougan earned 20 total points
ID: 6886932
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 10

Expert Comment

by:Clif
ID: 6888598
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
ID: 6888803
Because mdougan suggested a work around
0
 
LVL 10

Expert Comment

by:Clif
ID: 6888828
Oh.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

737 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