> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Declaring and Initializing Variables in the Same Line in VBA
- URL: https://nolongerset.com/declare-and-initialize-in-vba/
- Published: 2022-02-24T13:33:11.000Z
- Updated: 2026-05-08T12:59:26.000Z
- Description: You can't declare and initialize a variable in a single code *statement* in VBA, but you can do it in a single *line* of code with this handy trick.
- Author: Mike Wolfe
- Tags: Hidden Features, Intermediate, #Import 2026-05-20 02:59

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: 

```vba
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 (`:`). 

```vba
Dim i As Integer: i = 42
```

In the [parser](https://en.wikipedia.org/wiki/Parsing)'s eyes, the above line is equivalent to this more familiar code:

```vba
Dim i As Integer
i = 42
```

*Image by [Seksak Kerdkanno](https://pixabay.com/users/kerdkanno-1334070/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=906135) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=906135)*