Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Question on VBA AutoCAD arrays.

Status
Not open for further replies.

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.
 
Replies continue below

Recommended for you

You cannot ReDim Preserve an array without initially declaring it with a Dim statement. Also your function indicates that the array is initialized elsewhere - the point which is calling the function.

At this point you need to declare the array with a Dim statement - e.g. Dim MyArray().

After you have done this, enter the first value explicitly -
e.g.
ReDim MyArray(1 to 1)
MyArray(1)=3.245

Thereafter you can use the ReDim Preserve statement safely.

Good luck...


Mala Singh
'Dare to Imagine'
 
Thanks again Mala, I kinda figured that was what I would have to do. I was just hoping that I was missing something easy.
 
Sorry, forgot another item - why use a function to which an array needs to be passed? why not just place the two lines of code (in the function) in the original (calling) point...


Mala Singh
'Dare to Imagine'
 
LumpoDelMagnifico,

I am not good at languages other than English - but I suspect 'LumpoDelMagnifico' is Spanish?
Can you translate this for me? I just want to verify if it really means what I suspect it is meant to mean...I am not divulging my guess lest someone red-flags this post!!



Mala Singh
'Dare to Imagine'
 
I really wanted to set up a function that I could use to REDIM and add an item to a variety of arrays. That is instead of calling those two lines repeatedly, I could just do something like

AddItem MyStringArray, "LumpyRox"

or

AddItem MyIntArray, 1

But I think I was just overreaching
 
Lumpy is my real Nickname I aquired in College. One of the favorite hobbies of my classmates was finding variations on Lumpy Which means covered or filled with lumps(bumps) One of my favorites was Lumpo Del Magnifico I actually don't speak Spanish, but a loose translation would be Lumpy the Great or Lumpy the Magnificent. :]
 
Thanks for the update.
Fortunately, the meaning is not half as bad as I had imagined...
[smile]


Mala Singh
'Dare to Imagine'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor