Solved

Subscript out of range error

Posted on 2012-04-04
3
188 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 45

Expert Comment

by:Martin Liss
Comment Utility
Deleted.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

17 Experts available now in Live!

Get 1:1 Help Now