Ad space (banner)
Lesson 3 / 20

Variables (Dim)

This lesson covers the basics of declaring variables in VBA.

A variable is declared as Dim name As Type. The most common types are String (text), Integer (whole numbers), and Double (decimals).

The sample code declares two variables, Dim name As String and Dim age As Integer, and assembles a message by joining strings with the & operator.

A common early stumble is that Dim can be omitted at all. VBA runs without declarations by default, but writing Option Explicit at the top of a module heads off bugs caused by a forgotten declaration (usually a typo).

In professional work, always writing Option Explicit is treated as basic good manners for building macros you can rely on.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)