Can I dim multiple objects as Integer / Variant / etc. in one line?

2.5k views Asked by At

In VBA, can I Dim multiple objects as Integers in a single line in this concise fashion, or does this declare only d be an Integer?

Dim a, b, c, d As Integer
1

There are 1 answers

0
John Coleman On BEST ANSWER

You can test:

Sub test()
    Dim a, b, c, d As Integer
    Debug.Print TypeName(a)
    Debug.Print TypeName(b)
    Debug.Print TypeName(c)
    Debug.Print TypeName(d)
End Sub

The output in the immediate window:

Empty
Empty
Empty
Integer

The empty might be slightly confusing, but it makes it clear that only the last is an integer. Using F8 to step though the code, while viewing the results in the Locals Window is even more informative since then the types of a,b,c are explicitly given as Variant/Empty.