Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best VBA data type`key`=>`value` to save data same as PHP array

I'm working with VBA and need to save data in type key=>value to getting fastest; This data type help me cache responese text from http request, increase query speed. But I don't know what is the best way to do it? I need a data type same as php array with key=>value! Thank for help!

like image 210
Davuz Avatar asked Jul 13 '12 03:07

Davuz


People also ask

Which data type can hold any type of value in VBA?

Byte: A byte comes with a small capacity, and it can hold values ranging from 0 to 255. Integer: An integer is a beta version of the byte data type, and it can hold values ranging from -32768 to 32768. Any values that exceed this range will return an error.

What type is an array in VBA?

Types of VBA Arrays The VBA arrays can be categorized as follows: Static array. Dynamic array. One-dimensional array.

Why are collections better than arrays VBA?

collections can store only unique keys whereas arrays can store any value. collections have to store a couple (key, value) whereas you can store whatever in an array.


1 Answers

Have you looked at dictionary object?

It's available as part of the Microsoft Scripting Runtime. A clear example of how to add this is given by this SO answer.

Sub DictExample1()

Dim dict As Dictionary
Dim v As Variant

    'Create the dictionary          
    Set dict = New Dictionary

   'Add some (key, value) pairs
    dict.Add "John", 34
    dict.Add "Jane", 42
    dict.Add "Ted", 402

    'How many items do we have?
    Debug.Print "Number of items stored: " & dict.Count

    'We can retrieve an item based on the key
    Debug.Print "Ted is " & dict.Item("Ted") & " years old"


   'We can test whether an item exists
    Debug.Print "We have Jane's age: " & dict.Exists("Jane")
    Debug.Print "We have Zak's age " & dict.Exists("Zak")

    'We can update a value by replacing it
   dict.Item("Ted") = dict.Item("Ted") / 10

    Debug.Print "Ted's real age is: " & dict.Item("Ted")

   'We can add more items
    dict.Add "Carla", 23

   'And we can iterate through the complete dictionary
    For Each v In dict.Keys
        Debug.Print "Name: " & v & "Age: "; dict.Item(v)
    Next

End Sub

(Source: http://www.techbookreport.com/tutorials/vba_dictionary.html)

like image 187
Irfarino Avatar answered Oct 07 '22 16:10

Irfarino