With which the declaration or definition of the memory variable is assigned

Data types

The purpose of variables is to store and process values. You can write texts e.g. B. put together, you can calculate with numbers. To do this, the software must recognize whether a variable contains text or a number. So one expects z. B. that “lunch” and “eat” are combined to “lunch”, but “1” and “2” should result in “3” and not “12”. If we want it differently (i.e. an interpretation as text), we have to be able to tell the program specifically. Different computer programs provide different methods for this. In this regard, VBA is a so-called typed programming language. This means that every variable has a specific Data type it is determined from the outset what kind of values ​​can be stored in a variable.

a + b =? 
Text:Nooneat
Number:12
  3
Option Explicit
Sub HalloDu () Dim myText As String myText = "Hello World" Debug.Print myText End Sub

We already knew this example, but the variable declaration has been added so that the variable can only save text. The data type follows the keyword. As soon as you have typed, IntelliSense provides a fairly long selection list. The list varies depending on the application in which VBA is currently running. If you know "your" application, a lot will look familiar here. But initially we are only interested in them VBA internal data types:

Data types
TypeRange of valuesMemory requirementsInitial valueexample
BooleanYes / No values ​​(True or False)2 bytesFalsefemale = true
byteIntegers 0 to 2551 byte0Age = 42
IntegerIntegers -32,768 to 32,7672 bytes0Year of construction = 1950
LongIntegers -2,147,483,648 to 2,147,483,6474 bytes0Customer number = 23
LongLong ¹Integers approximately ± 9 trillion8 bytes0People =
Currencyscaled integers approx. ± 9 trillion, 4 decimal places8 bytes0Euro = -47.11
singleFloating point number, single precision4 bytes0rounded = 1.5
DoubleFloating-point number, double precision8 bytes0Result = 1.75
DateDate (Jan 1, 100 to Dec 31, 9999) and time8 bytes30.12.1899 00:00Today =
StringTextsNumber Character * 2""Info = "Hello world"
Objectdepending on the object4 bytesNothingSet BMW = car
Variantdepending on the current contentdifferentlyEmptymiscellaneous = -1

¹ Only available on 64-bit systems

The decision for a data type depends on whether you want to save texts or numbers. There is also the syntax for texts of fixed length, e.g. B. if the string should always contain exactly one character. When it comes to numbers, a distinction is made between integers and floating point numbers. There are several data types for this (integers:, and. Floating point numbers: and). rather exists for reasons of compatibility and should no longer be used today. is internally an integer that is divided by 10000 and thus has 4 fixed decimal places. In contrast to "normal" floating point numbers, this is a very precise calculation method and is therefore particularly recommended in connection with amounts of money. -Variables can have any value. An instruction without a data type declares the variable as a variant.

As soon as a variable has been declared, it is assigned an initial value and storage space. Numeric variables are assigned the value 0, strings an empty string "". Fixed-length strings are assigned a character sequence consisting of the control character, as often as the string is long. A variant variable is given the special value, which indicates that it has not yet been assigned a value. You can also reassign it later in order to put it back into a “virginal state”. A variant can also contain the special value to indicate that the variable does not contain valid data.

-Variables are only addressed much later in the tutorial. It should only be mentioned here that the initial value is a special value to indicate that the variable does not refer to an object.

In practice it is seldom of interest how much memory space a variable consumes - unless you want e.g. B. save an entire database in variables at once. With and you can see why these data types can have a performance problem: their memory requirements change depending on the content of the variable. “Under the hood” the computer has to rearrange a lot in the memory when the content changes. In addition, VBA must also remember which data type the current value is to be interpreted as. While therefore in the environment z. B. from Microsoft Access the data type is frowned upon, some users of Microsoft Excel often used to use variables like in a non-typed script language.