Solved

Subscript out of range error

Posted on 2012-04-04
3
193 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
[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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

752 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