Thursday, 15 June 2017

What is Scripting.Dictionary? What is Array? in VBA excel macro



Hello all,
What is Scripting.Dictionary?
Nice question … don’t worry am here to answer. Before that What is Array?
Array is a data structure (Don’t ask what is data Structure) that contain group of element with the same data type.
Example:
Dim ArrayExample
ArrayExample=array(“Fail” ,”Mark”)


Or


ArrayExample(0)=”Fail”
ArrayExample(0)=”Mark


(If you need to know about array and dimension comment below)


Now Dictionary :


Dictionary object is similar to PERL associative array…(Google it What is PERL array?)
Dictionary object can stored any value in the array and each stored items associated with a
unique key. We can retrieve data using key.( see the example then you will understand more)


Example:
Dim DictionaryExample   (Don’t write Dim DictionaryExample() )
Set  DictionaryExample = CreateObject(“Scripting.Dictionary”)


DictionaryExample.Add  “key”, “Fail”
DictionaryExample.Add  “keeey”, “Mark”


See the example now and read the explanation again.


(I know about your laziness read here) The value “Fail” and “Mark” stored in array, the stored items “Fail” and “Mark” associated with a unique key “key” and “keeey”. We can retrieve data “Fail” and “Mark” using key “key and Keeey”.


Program for array: (Do copy past like a programmer)





Sub ArrayExampleProgram()
Dim ArrayExample
ArrayExample = Array("Fail", "Mark")

Debug.Print ArrayExample(0)
Debug.Print ArrayExample(1)
End Sub

Sub ArrayExampleProgram1()
Dim ArrayExample(1)
ArrayExample(0) = "Fail"
ArrayExample(1) = "Mark"

Debug.Print ArrayExample(0)
Debug.Print ArrayExample(1)
End Sub 

Program for Data Dictionary:









Sub DictionaryExampleProgram()
Dim DictionaryExample
Set DictionaryExample = CreateObject("Scripting.Dictionary")


DictionaryExample.Add "key", "Fail"
DictionaryExample.Add "keeey", "Mark"


Debug.Print DictionaryExample.Item("key")
Debug.Print DictionaryExample.Item("keeey")
End Sub




(bye byeeeeeeeeee......)
 

No comments:

Post a Comment