Thursday, November 6, 2014

How to use array in VBA (macro)

Thanks to my friend Pravin who asked me about his array query and lead me to write this post. So basically, an array is bunch of a same kind of variables stored under one variable. You should know what is variable and their types before getting into array.
So an array can have any store number of depends on RAM size of your machine.
So enough theory just check an example below:
Ex1 :

Sub MyFirstArray ()
Dim MyArray (2) as Integer
MyArray (0) = 21
MyArray (1) = 30
Msgbox " First array value = " & MyArray (0)
Msgbox " Second array value = " & MyArray (1)
End Sub

You can see first line in MyFirstArray procedure is declaration of array means you told program about the length of array you would gonna use. Array always start with 0 by default, thus in the second line I have use.
MyArray (0) = 21
Meaning first place of my array which is having an index of 0,  will store value 21.
similarly second place of array that is last place, which is having an index of 1 will a store value 30.
This is how we can assign a value to an array.
Now how to get or access value from array so in 4th and 5th line of procedure we have show stored values in message box.
So this is about basic you should know to start with array.
So please try with other types of variables and get back to me in case of and query.
But my friend Pravin's actual query was how to use dynamic array is in my next post.......

No comments:

Post a Comment