Consulting

Results 1 to 6 of 6

Thread: Solved: better Array support??? to avoid Dim, ReDim, Preserve etc

  1. #1

    Question Solved: better Array support??? to avoid Dim, ReDim, Preserve etc

    Hi guys,

    Is there an add-on available that provides an easier way to do Dynamic Array type stuff? It seems in base VBA you need to be using Dim, ReDim, Preserve etc as the arrange grows/shrinks etc

    What I'm really after is something like this:

    Dim myArray() As String
    
    myArray.add("first item")
    myArray.add("second item")
    myArray.add("third item")
    
    if myArray.contains ("second item") Then
        Do XYZ...
        myArray.remove("second item")
    end if
    
    remainingSize = myArray.length
    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You would need to write yourself a simple little class that does all of those things for you. Alan Beban has written an array add-in, but it has always looked inordinately complex to me. But remember, the main problem is knowing the size and so on, you will probably have to control that outside of an utility.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What 'stuff' do you actually want to do?

  4. #4
    basically by this I meant I want to add/remove values from an in-memory list as part of the program, but I was really hoping not have to worry about all this ReDim'ing stuff. Really just want a basic collection object I can add/remove items from dynamically and let the language worry about size 'under the bonnet'.

    Any hints/ideas welcome.

    I'll google the array add-in that xld mentioned too.

    PS. I wonder if there is a good Ruby interface to the Excel API's.

  5. #5
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Hi C,
    In your visual basic editor go to the Tool Menu, Click "References" and Place a check mark next to "Microsoft Scripting Runtime" you can then use their "Dictionary Object" I think you will find this meets exactly the specification you laid out above.
    [VBA]Public Sub ExampleUse()
    Dim myCollection As Scripting.Dictionary

    'If you don't want
    myCollection "first item"
    myCollection "second item"
    myCollection "third item"

    If myCollection.Exists("second item") Then
    'Do XYZ...
    myCollection.Remove ("second item")
    End If
    remainingSize = myCollection.Count
    End Sub[/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  6. #6
    this is excellent Oorang! Exactly what I need.

    Thanks heaps for pointing this out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •