ReDim an array AFTER it's loaded into another array

G

Guest

HI All,

I have an array whose elements are other arrays. I use option base 1, and
things are OK.
Once a "master" array is formed, it looks like the component arrays cannot
be re-dimmed.

The ReDim VBA help is silent on this ability.

in a procedure:

dim master(2)
dim elem1() as string
redim elem1(3)
dim elem2() as integer
redim elem2(3)

values are put into elem1 and elem2 and then:

master(1) = elem1
master(2) = elem2

I was testing trying to ReDim " elem1" and "elem 2" AFTER the
master was "loaded".

I get an immediate compiler 'red ink' error when I type:
Redim master(1)(4)
Note: ReDim master(3) to add another array does me no good.

I guess I have to leave "room" in elem1 and elem2 before they
are loaded into the master, or unload the master to the
elemental arrays, ReDim them, and finally re-load back
to the master.

Am I correct, or is there another way?
Thanks,
 
G

Guest

Sub B()
Dim master(1 To 2)
Dim v As Variant, e As Variant
e = Array(1, 2, 3)
master(1) = e
v = master(1)
ReDim Preserve v(1 To UBound(v) + 10)
master(1) = v
Debug.Print UBound(master(1), 1)
End Sub

is the way I do it.

--
Regards,
Tom Ogilvy


I was testing trying to ReDim " elem1" and "elem 2" AFTER the
master was "loaded".

I get an immediate compiler 'red ink' error when I type:
Redim master(1)(4)
 
G

Guest

I like Tom's code but if you want to stick with you roriginal line of thought
then this might help...

Sub test()
Dim master(2)
Dim elem1() As String
ReDim elem1(3)
Dim elem2() As Integer
ReDim elem2(3)

master(1) = elem1
master(2) = elem2

ReDim Preserve elem1(4)
master(1) = elem1
End Sub
 
G

Guest

Tom -
Thanks a million, I will try it as soon as I finish typing this. By the
way, as a navigate the community question, I used to be able to double click
on my subject heading, and up would pop a full screen with all the replys and
I could click on the reply was/was not helpful button.
Now, when I dbl click, I get the screen but there's no scroll bar on the
right so it's immovable. Is it my computer, or has there been some kind of a
change by MSoft?
thanks again.
Neal
 
G

Guest

Jim -
Yes, in the macro where the master is created that is indeed what I do.
what I neglected to put into the original post was that the master is passed
from macro to macro. ( it's a small data base containing facts gleaned from
some of the worksheets in the workbook that's at the hub of my application)
Once outside the macro where the master was created, the 'elemental'
arrays are gone and can't be "addressed". The master is my shorthand way of
accessing a lot of data without dragging around all of the elemental arrays.
(There are 12 of them in actuality) I may have to go back my original design
which was one two-dimension array, so I can redim a column to add what I
need. I'll be tryout out Tom's answer tonight. Soon.
Thanks for your reply.
 
G

Guest

If that is the case you could copy out the array into a new array and then
redim that and put it back...

Sub test()
Dim master(2)
Dim elem1() As String
ReDim elem1(3)
Dim elem2() As Integer
ReDim elem2(3)

master(1) = elem1
master(2) = elem2

Call SubTest(master)
End Sub

Sub SubTest(ByRef master() As Variant)
Dim elem1() As String

elem1 = master(1)
ReDim Preserve elem1(4)
master(1) = elem1
End Sub

A little cumbersome but it does the trick...
 
G

Guest

Jim - sure could, I got Tom's suggestion to work with pretty close to real
variable names, and have put parts of the code under his thread if you're
interested.
thanks again for your continued support.
 
G

Guest

Tom -
Gorgeous, just gorgeous
Here are some parts of your code after substituting my real var names.
Works Great - arrays are powerful stuff. It took me a minute or two to
realize your v variable was a holding area I could put into the loop you'll
see below.
thanks again


' RFA = route facts array, old route qty was 3
' there are actually 15 facts I need to know about each route
For iDataType = RFAOrg2IdCol To RFAnaPartCol
'your v is my vReDimHold, the master is the Route Facts Array
vReDimHold = vRteFactsAy(iDataType)
ReDim Preserve vReDimHold(UBound(vReDimHold) + 5)
vRteFactsAy(iDataType) = vReDimHold
Next iDataType

' sample New route worksheet in the district workbook
vRteFactsAy(RFAOrg2IdCol)(4) = "district"
vRteFactsAy(RFAfullNaCol)(4) = "New-Route"
vRteFactsAy(RFAnaPartCol)(4) = "Abbrv"

' Unloading the data to a worksheet for display, wow!
RouteQty = 4
SheetRow = 6
For Index = 1 To RouteQty
For iDataType = RFAOrg2IdCol To RFAnaPartCol
Cells(SheetRow, iDataType) = vRteFactsAy(iDataType)(Index)
Next iDataType
SheetRow = SheetRow + 1
Next Index
 
N

NickHK

Assuming the count in elem1 match the count elem2 (you'll always these 2
pieces of data together), why not use an array of custom types. e.g.

Public Type Data
Element1 as string
Element2 as long
End Type

Dim AllData() As data

redim AllData(1 to 10)

alldata(1).element1="Some value"
alldata(10).element2=5

redim preserve alldata(1 to 100)

NickHK
 
G

Guest

Nick -
I am not familiar with the Type Data construct, but I'm gonna learn.
Your suggestion is elegant in its simplicity. I will try it.
Thanks.
 
G

Guest

Dear Nick,
I've looked at the Type constuct, and it's something I will use in my
app, but not for the question I posted. You could not know from my posting
('cuz I didn't state it as a requirement) how I use this data. I don't see
how I can put both the kind of data AND the route (which is what my app is
about) in a loop since the kind of data is in the .name structure. The ReDim
requirement is nicely met however and I will use it in other places, so
thanks much for the heads up. See the code below for what I don't think is
possible with the user defined data contstruct.
Neal

RouteFacts.Org2ID = "District-811"


' this unloads the route facts array (the master in my posting) to a
worksheet.
RouteQty = 4
SheetRow = 1
For Index = 1 To RouteQty
' RFA is the route facts array
For iDataType = RFAOrg2IdCol To RFAnaPartCol
' RFAOrg2IdCol points to the "elem1" array in my posting

Cells(SheetRow, iDataType) = vRteFactsAy(iDataType)(Index)
Next iDataType
SheetRow = SheetRow + 1
Next Index
 
N

NickHK

Neal,
Without knowing the actual data structure you need I would guess something
like this:
private type RteFactsData
.RteName as string
.RteInfo1 as long
'.RteInfo2 as string 'etc depending on how many pieces of data you need
end type

Dim vRteFactsAy() as RteFactsData
'create the required number of elements somewhere
Redim vRteFactsAy(10)

'Then in your loop below, you can access the required data as
Cells(SheetRow, iDataType) = vRteFactsAy(iDataType).RteName
or
Cells(SheetRow, iDataType) = vRteFactsAy(iDataType).RteInfo1
depending on which piece of data "Index" refers to.

The benefit of this kind of code, is that it makes it more clear which piece
of data you are referring to. Compare:
vRteFactsAy(iDataType).RteInfo1 to vRteFactsAy(iDataType)(Index)

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top