Search code examples
vbaexcelvariable-declaration

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


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

Solution

  • 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.