×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Simple Question on VBA AutoCAD arrays.

Simple Question on VBA AutoCAD arrays.

Simple Question on VBA AutoCAD arrays.

(OP)
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.

RE: Simple Question on VBA AutoCAD arrays.

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'

RE: Simple Question on VBA AutoCAD arrays.

(OP)
Thanks again Mala, I kinda figured that was what I would have to do. I was just hoping that I was missing something easy.

RE: Simple Question on VBA AutoCAD arrays.

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'

RE: Simple Question on VBA AutoCAD arrays.

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'

RE: Simple Question on VBA AutoCAD arrays.

(OP)
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

RE: Simple Question on VBA AutoCAD arrays.

(OP)
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. :]

RE: Simple Question on VBA AutoCAD arrays.

Thanks for the update.
Fortunately, the meaning is not half as bad as I had imagined...


Mala Singh
'Dare to Imagine'

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources