Declaring and Initializing Variables in the Same Line in VBA

VB.NET is a fundamentally different language than VB6/VBA, but on the surface the two languages share a lot of common syntax.

One nice feature that VB.NET has that VBA is missing is the ability to assign a value to a variable in the same line where you declare it:

Dim i As Integer = 42

Unfortunately, the above code will generate a syntax error in VBA.

However, if you really like that approach to variable initialization, you can get something very similar in VBA using the end-of-statement colon character (:).

Dim i As Integer: i = 42

In the parser's eyes, the above line is equivalent to this more familiar code:

Dim i As Integer
i = 42

Image by Seksak Kerdkanno from Pixabay