Defining a Dyanmic Array in User Defined Type

C

CK

How would you define a dynamic array in a UDT? For instance, how could I do
something like:

Type InventoryList

nItems As Long

inventory(1 to nItems) As Long

End Type

Cheers.
 
G

Gary''s Student

Dim inventory() As Long
Dim nItems As Long
nItems = 6
ReDim inventory(1 To nItems) As Long
 
D

Dave Peterson

I'd use:

Option Explicit
Type InventoryList
nItems As Long
Inventory() As Long
End Type
Sub aaa()
Dim myInventory(1 To 3) As InventoryList
Dim iCtr As Long
Dim nCtr As Long

For iCtr = LBound(myInventory) To UBound(myInventory)
myInventory(iCtr).nItems = 3
ReDim myInventory(iCtr).Inventory(1 To myInventory(iCtr).nItems)
'put some values in
For nCtr = 1 To myInventory(iCtr).nItems
myInventory(iCtr).Inventory(nCtr) = 2 * nCtr
Next nCtr
Next iCtr
End Sub
 
C

CK

This is what I did on my laptop (Excel 2007) last night but it wouldn't work.
When I try the same thing this morning on my work PC (Excel 2003) then it
works. Let me re-try it on my laptop tonight to see.

Thanks Dave for your solution.
 

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