

In time the list of dim statements in your language will narrow so you use the same shortened dim statements over and over again. The use of variables helps to generate a list of variables in a single place where both you and others can refer. Understanding the upper and lower bounds of each data type helps to decide which variable to use in which situation but this comes with practice and time. The above is one way to reference an integer.Ī list of possible variables is in the table below.
#Dim definition code
Don’t confuse the worksheet code name with the worksheet name.ĭeclare an Integer Variable Dim i as IntegerĪn integer is a whole number and i is a very common variable for integers. The above sets the term ‘sh’ to equal the worksheet code name Sheet1. A Sheet is is a worksheet and is mostly used to denote a worksheet inside the active workbook. The above declares the term ‘sh’ as a Sheet variable.

The above statement means that rng is equal to and now instead of typing you simply type rng.ĭeclare a sheet Variable Dim sh as Worksheet Set rng = īecause a range is part of the object hierarchy the Set statement needs to be added before the rng. See below for the output for the rng variable. The above declares the term ‘rng’ as a Range variable, a Range is a cell or grouping of cells. txt = “Cowboy”Īfter this line instead of typing Cowboy - txt is typed - in the VBA language the two are now the same thing and every time instead of typing Cowboy you would type txt.ĭeclare a Range Variable Dim rng as Range See below for the output for the txt string. The above declares the term ‘str’ as a String variable, a string is text. An example of some commonly used Dim statements are as follows:ĭeclare a String Variable Dim str as String If you declare the variable up front you only have to use the declared variable rather than the range referenceĪ listing of Dim statements helps users read your code. For example a range might need to be referred to 10 times within a macro. It is a way to refer to the declared term rather than the same object over and over again.

The Dim statement is put at the start of a VBA module after the Sub statement (short for Subroutine). Dim in the VBA language is short for Dimension and it is used to declare variables. What does Dim mean in the VBA language? This is a question that I get asked a lot in by students in my courses.
