Solved

Subscript out of range error

Posted on 2012-04-04
3
192 Views
Last Modified: 2012-04-05
Hi,

I'm trying to redim a dynamic multi-dimensional array. However, no matter how I frame the redim statement, it doesn't work.

Sub test()

Dim yabba() As Variant

ReDim yabba(10, 1)

yabba(0, 0) = "a"
yabba(0, 1) = 1
yabba(1, 0) = "b"
yabba(1, 1) = 2
yabba(2, 0) = "c"
yabba(2, 1) = 3

ReDim Preserve yabba(3)

MsgBox (UBound(yabba))

End Sub

Open in new window

0
Comment
Question by:WhyDidntItWork
  • 3
3 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37809416
Deleted.
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 37809438
From Help:


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.
So you could Redim Preserve (10, 2) if you wanted to.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37809482
You could do this:

Option Explicit
Private Type MyType
    Var1 As String
    Var2 As Integer
End Type
Private Sub Form_Load()
Dim yabba() As MyType

ReDim yabba(10)

yabba(0).Var1 = "a"
yabba(0).Var2 = 1
yabba(1).Var1 = "b"
yabba(1).Var2 = 2
yabba(2).Var1 = "c"
yabba(2).Var2 = 3

ReDim Preserve yabba(3)

MsgBox (UBound(yabba))

End Sub

Open in new window


My profile contains links to some articles that may interest you.
Marty - MVP 2009 to 2012
0

Featured Post

Independent Software Vendors: 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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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