Solved

how do i redim a multi dimension array

Posted on 2004-03-29
8
350 Views
Last Modified: 2010-05-02
i am trying to run this piece of code.  it works fine if the array is only 1 column wide but now i need it to have 2 columns it runs 1 loop ok then stops with the error "subscript out of range"

For i = 1 To UBound(arrParagraph)
ReDim Preserve arrParagraphData(i, 2)
arrParagraphData(i, 1) = arrParagraph(i)
arrParagraphData(i, 2) = 5
cmbParagraph.AddItem arrParagraph(i)
Next
0
Comment
Question by:Marcusw
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 10703479
You may want to do this

ReDim Preserve arrParagraphData(UBound(arrParagraph), 2)
For i = 1 To UBound(arrParagraph)
arrParagraphData(i, 1) = arrParagraph(i)
arrParagraphData(i, 2) = 5
cmbParagraph.AddItem arrParagraph(i)
Next
0
 
LVL 3

Expert Comment

by:kenspencer
ID: 10703581
Hi,
Perhaps you should move the ReDim out of the loop.

Dim x as Integer

x = UBound(arrParagraph)
Redim Preserve arrParagraphData(x, 2)

For i = 1 To x
   arrParagraphData(i, 1) = arrParagraph(i)
   arrParagraphData(i, 2) = 5
   cmbParagraph.AddItem arrParagraph(i)
Next

Ken
0
 
LVL 3

Expert Comment

by:kenspencer
ID: 10703587
woops ... looks like I was too slow ...

Ken
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10704538
>>ReDim Preserve arrParagraphData(i, 2)<<
You cannot do that.  You will have to change it to something like:
ReDim Preserve arrParagraphData(2, i)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10704553
Also, please followup on these old open questions:
1 02/02/2004 500 populate xml with selected item and remo...  Open Visual Basic
2 02/03/2004 500 Remote access denied using odbc 3.51.06 ...  Open Mysql
3 02/04/2004 500 add and remove elements in an existing x...  Open XML
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10704582
You can do that if you define

Dim arrParagraphData
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10705122
EDDYKT,

>>You can do that if you define<<
From BOL:
<quote>
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.
</quote>
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10706274
acperkins,

you are right if you've already defined that as 2 dimension array.

However, I just declare as variant
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now