19

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 first As clause encountered after its variable name part.

However, when I checked with the debugger or MsgBox VarType(a) output, this is not the case.

enter image description here

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)

2 Answers 2

31

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
5
  • 5
    Also see the correct documentation for VBA: Dim statement
    – Pᴇʜ
    Commented Mar 8, 2019 at 9:29
  • Uh-huh, it clearly explains what I encountered. I did not realize there are many variations of Visual Basic language! Commented Mar 9, 2019 at 12:55
  • 2
    this is an insane language feature.
    – NingNing
    Commented Apr 25, 2020 at 15:35
  • @NingNing Bug...
    – Brethlosze
    Commented Mar 13, 2023 at 21:30
  • 2
    Have always wondered about this and thought I would finally do some checking and find out -- your answer made this v. easy and gave a clear, definitive answer, thanks!! // Also, thanks to @Pᴇʜ for the link to the Dim documentation.
    – Martin
    Commented Feb 9 at 20:31
8

In VBA, when you declare

Dim a, b, c As Single

What it does is equivalent to this:

Dim a As Variant, b As Variant, c As Single

I believe the best practice is to always declare variable in a separate row. It prevents this bug and also allows for faster scanning of the code. So the code would look like this:

Dim a As Single
Dim b As Single
Dim c As Single
6
  • Very well pointing this as a Bug instead of as a Feature
    – Brethlosze
    Commented Mar 13, 2023 at 21:30
  • @Brethlosze This answer is not presenting this language feature as a bug. It is merely pointing out that other bugs can be prevented by adhering to a certain style.
    – GSerg
    Commented Mar 9 at 15:51
  • @GSerg Your comment is merely denying, that we should understand the word "bug" in this answer, and replace it by the word "feature". This is not rational at all. I suggest you to focus on concrete ideas while writing comments on this site.
    – Brethlosze
    Commented Mar 11 at 2:29
  • 1
    @Brethlosze My comment is pointing out that you have misrepresented the opinion expressed in this answer for future readers, which is a rather concrete idea. You are claiming that this answer is calling this style of declaration a bug. It isn't calling it a bug. Thus your comment is wrong and should be removed.
    – GSerg
    Commented Mar 11 at 9:00
  • 1
    @Brethlosze Your misguided condescension is not appreciated. The words "this bug" used in the answer refer to the bug the programmer commits when they decide to declare the variables on a single line without understanding the implications, not to the intricacies of the language feature. Using your reasoning, your comment is a bug because it literally has the word bug in it.
    – GSerg
    Commented Mar 17 at 0:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.