1
Sub foobar()

Dim wine As Variant
wine = Array("Red", "White", "Rose", "Sparkling")

Dim spirits As Variant
spirits = Array("Vodka", "Whiskey", "Rum", "Gin")

Dim beer As Variant
beer = Array("Ale", "Lager", "Pilsner", "Stout")

Dim inventory As Variant
inventory = Array(wine, spirits, beer)

Range("A1") = inventory(1)

End Sub

My current idea is to put the name of the array in the array,

wine = Array("wine", "Red", "White", "Rose", "Sparkling")

spirits = Array("spirits", "Vodka", "Whiskey", "Rum", "Gin")

beer = Array("beer", "Ale", "Lager", "Pilsner", "Stout")

inventory = Array(wine, spirits, beer)

Therefore inventory(x)(0) will always return the name of the array that way.

I want "A1" to show the name of the Dimension, i.e. spirits, but spirits itself is an array, so in "A1" I get the equivalent of inventory(1)(0).

Is there a better way to get the wine, spirits, or beer Dimensions returned as their name, than to include the name in the array (as above)?

1
  • They are variable and not objects. You will need to store another array with the desired names as strings. Commented Mar 18, 2016 at 18:22

1 Answer 1

3

If you want to reference things by name then a Dictionary is one solution.

Sub foobar()

    Dim wine As Variant
    wine = Array("Red", "White", "Rose", "Sparkling")

    Dim spirits As Variant
    spirits = Array("Vodka", "Whiskey", "Rum", "Gin")

    Dim beer As Variant
    beer = Array("Ale", "Lager", "Pilsner", "Stout")

    Dim inventory As Object, k
    Set inventory = CreateObject("Scripting.Dictionary")
    With inventory
        .Add "Wine", wine
        .Add "Spirits", spirits
        .Add "Beer", beer
    End With

    'access a specific item
    Debug.Print Join(inventory("Beer"), ",")

    'loop over items
    For each k in inventory.keys
        Debug.Print k, Join(inventory(k), ",")
    Next k

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.