Variables in VBA:
1)Variables in VBA are declared by using Dim statement. VBA like any other programing language as many variable types. To list a few Integer,Double,String,Long,Variant,Range etc.
If a string variable needs to be declared it can be declared as
Dim strTest as String
The above style of declaring the variable is called Hungarian notation. In this variable name is prefixed by the variable type. Another variable is Range which is different from other programming languages. Range variable is declared similar to any other variable like Dim rngTest as Range. Any range in excel can be used in VBA by setting that range to this range variable. To play with this range first set this range as :
Set rngTest=Range("A1:B3")
Now the range variable rngTest can be used to access the variables in this range.
rngTest(1,1)->This will give you the value in cell A1.
2)Variant is one powerful variable in VBA. It can hold different variable types. So Variant is used when the user is not sure about the variable type. Do not try to use variant as the preferred variable. It might seem to be easy and obvious choice but it can be dangerous considering the code performance.
If you use Variant as data type the compiler will take more time in identifying the Variable as compared to a variable of specific type.
If you are using Variant as variable type for variable which stores always one integer value. Then there is one is disadvantage that you might not be able to see the value of the variable while debugging. There might also Issue where lot of "type mismatch" errors will be missed if you use Variant as datatype.
An array variable can be declared as variant type.
Dim arrTest as variant.
The lower bound of this array is 0. To change the upper the bound of this array it can be redeemed.
Redim arrTest(1,4)
Now keep in mind one thing that lower bound always starts with 0. In case you have to change the lower bound you can
Option base 2
This will take the lower bound as 2. By default the lower bound is 2.
This blog is about Technology. One ultimate destination for tutorials on programming languages, exploring about complex situations in different programming languages. Also Knowing about most wanted technologies now and in Future. The Major content as of now will be focussed on Excel and Visual Basic for Applications(VBA). Have a happy reading...!!
Tuesday, October 27, 2009
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
About Me
- Ravindra Singh Yadav
- Hello World!. I am an aspiring blogger, striving towards knowing more about various Technologies and innovating about it. I am full of Energy, Enthusiasm to learn more and more....
No comments:
Post a Comment