According to this documentation by Microsoft, the following code can be used to make sure;
a, b, and c are all Single; x and y are both Double
Dim a, b, c As Single, x, y As Double, i As Integer
> a, b, and c are all Single; x and y are both Double
The logic behind this is as follows
You can specify different data types for different variables by using a separate
As
clause for each variable you declare. Each variable takes the data type specified in the firstAs
clause encountered after its variable name part.
However, when I checked with the debugger or MsgBox VarType(a)
output, this is not the case.
As you can see, it appears that As
is only working for the variables right before itself, ie., c
, y
and i
. All others are Variant/Empty and VarType
returns 0.
Is this just the documentation being wrong, or am I missing something obvious?
Microsoft Visual Basic for Application 7.1.1056 Excel 2016 (Windows 10)
Declaring multiple variables You can declare several variables in one declaration statement, specifying the variable name for each one, and following each array name with parentheses.
When declaring variables, you usually use a Dim statement. A declaration statement can be placed within a procedure to create a procedure-level variable.
To declare a variable, type Dim, the variable name, and the variable type… somewhat like this… If you don't specify the type of variable, as shown in the first example at the top of this post, VBA declares the variable as a Variant type. A Variant can accept any type of variable.
In Go, it is possible to declare multiple variables in the same line.
The documentation you've linked to isn't wrong, but it's written for VB.NET and not VBA.
In VBA, as you've observed, any variable declarations that aren't immediately followed by As <type>
will be Variant
.
Therefore you'd need to write:
Dim a As Single, b As Single, c As Single, x As Double, y As Double, i As Integer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With