LumpoDelMagnifico
Electrical
- Sep 22, 2004
- 35
I am trying to use A dynamic array in VBA and I am having a problem. I wrote the following function to Redim my Dynamic array and add an element to the last spot.
Function DarrayAdd(Darray As Variant, Ditem As Variant)
ReDim Preserve Darray(UBound(Darray) + 1)
Darray(UBound(Darray)) = Ditem
End Function
Real Simple Huh? The problem is that Ubound returns Error 9 "Subscript out of Range" when I run this. I can declare the initial array with a value of 1 and then Ubound works but I get a message telling me the array is locked or in use. If I pass the Array by value (Byval Darray as Variant.. I can work with it, but it doesn't affect the initial array and the Gymnastics I would have to do to pass it back make this not worthwhile. Does anyone have any idea what I am doing wrong? Basically I need a quick and easy way to expand my array by 1 and add an item to that spot.
Function DarrayAdd(Darray As Variant, Ditem As Variant)
ReDim Preserve Darray(UBound(Darray) + 1)
Darray(UBound(Darray)) = Ditem
End Function
Real Simple Huh? The problem is that Ubound returns Error 9 "Subscript out of Range" when I run this. I can declare the initial array with a value of 1 and then Ubound works but I get a message telling me the array is locked or in use. If I pass the Array by value (Byval Darray as Variant.. I can work with it, but it doesn't affect the initial array and the Gymnastics I would have to do to pass it back make this not worthwhile. Does anyone have any idea what I am doing wrong? Basically I need a quick and easy way to expand my array by 1 and add an item to that spot.