Important : Declaring multiple Variables with one Dim Statement
As you know in VBA variables are declared using Dim Statement. You have also learnt about how to declare a variable in VBA in the previous articles. You have also learnt about Implicit and explicit option variables.
Here is a question: In below image there are two ways of declaring three variables VAR1, VAR2 and VAR3 as an integer type. Is there any difference between two ways of declarations?
Answer: Yes there is a difference between both the declaration. To know what is the difference read the explanation
In VBA one can declare more than one variables with a single Dim statement as written below:
Dim VAR1, VAR2, VAR3 As Integer
From the above way of declaration, usually we think that all the above 3 variables are declared as “Integer” Type. But this is NOT correct. Only the last variable VAR3 is declared as Integer and rest 2 variables are declared as Variant Type. (Note: Default type of variables in VBA is Variant). The above declaration is functionally equivalent to the following:
Dim VAR1 As Variant
Dim VAR2 As Variant
Dim VAR3 As Integer
Still if you want to declare all the three variables of Integer type with one Dim Statement then As Type modifier must be used for each variable declared with the Dim statement as below:
Dim VAR1 As Integer, VAR2 As Integer, VAR3 As Integer
Coming to the question on top, with the explanation given above it is clear that both the declarations are NOT the same but the below two ways of declarations are exactly same:
My name is Vishwamitra Mishra. Friends Call me Vishwa. This blog is authored by me. I am an Excel Geek. Well, this blog talks a lot about my passion in Excels & Macros so I’ll not talk about it :) I am very much passionate about traveling & quite recently discovered that I am a good photographer too..:P
AND GET A FREE!
E-BOOK FOR EXCEL VBA BEGINNERS
DON'T MISS ANY NEW ARTICLE !
Get your FREE! E-Book & Have Every New Article Delivered Straight To Your Email-Box