Can I dim multiple objects as Integer / Variant /

2019-02-25 00:44发布

问题:

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:

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.