|Hoss||I just blew the heck out of this problem I've been having for quite some time in my code. I couldn't quite ReDim a nested User Defined Type without getting a 'subscript out of range' error!
For those of you that don't know; A User Defined Type (UDT) is a data type that you create yourself, hence User Defined. Here's an example of a UDT:
Public Type Chumps
GirlChumps() as String
BoyChumps() as String
That is our first data type that we'll be using. Notice that the GirlChumps() and BoyChumps() arrays have no ranges! The purpose of this post is to show you how to redim those arrays, so just wait a moment and I'll get to it.
We need another UDT:
Public Type Classrooms
Subject as String
Classmates as Chumps
Notice I defined the variable Classmates as Chumps, our previously defined UDT.
Dim Schedule() as Classrooms
Okay, now that we've got our variables dimmed, let's look into how we ReDim (Resize) our arrays with no ranges!
"How can we do this?" You might be asking. With! That's how!
ReDim Schedule(1) 'This is our school schedule. Unfortunately this
'school is under-budgeted, so we can only attend
'two classes a day.
With Schedule(0) 'Our first class
ReDim .Classmates.GirlChumps(1) 'Only two girls in this class
ReDim .Classmates.BoyChumps(1) 'Only two boys in this class
With Schedule(1) 'Our second class
As you can see, using WITH we can RedDim our undefined arrays without messing with the original Schedule() range. Alternatively, you can use a loop to iterate through the Schedule and Chumps arrays to more effectively set the ranges. I didn't do that... I typed it all out, for simplicity.
Now that we have it all ReDimmed, we can now define the arrays!
Schedule(0).Classmates.GirlChumps(0) = "Lacy"
Schedule(0).Classmates.GirlChumps(1) = "Melissa"
Schedule(0).Classmates.BoyChumps(0) = "Todd"
Schedule(0).Classmates.BoyChumps(1) = "Alex"
Schedule(0).Subject = "Math"
Schedule(1).Classmates.GirlChumps(0) = "Clarise"
Schedule(1).Classmates.GirlChumps(1) = "Jenna"
Schedule(1).Classmates.BoyChumps(0) = "Chris"
Schedule(1).Classmates.BoyChumps(1) = "Matt"
Schedule(1).Subject = "Science"
Again, a loop could have been used to define the arrays, but for simplicity I typed it out manually.
I hope this post provided more help than hinder! Sorry if it's a bit confusing!