Solved

Subscript out of range error

Posted on 2012-04-04
3
189 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 46

Expert Comment

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

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find word and 6 digit number 22 97
Unable to open excel in 2016 is slow 4 21
Excel case statements 3 24
Auto populate in Cascade dropdown 3 25
This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

930 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

11 Experts available now in Live!

Get 1:1 Help Now