About Variable Types Excel(Excel) Macro(VBA)

List of variable types Excel Macro (VBA)
The article contains advertisements.

This section describes the types used to declare variables in "Dim Variables".

Typical variable types

Here are some typical variable types that you only need to remember. I use almost only these.

Variable Typesrange of data that can be stored
stringcharacter string
longInteger (-2,147,483,648 to 2,147,483,647)
doubleminority
Negative values: approximately -1.7 x 10 to the 308th power to the -324th power of approximately -4.9 x 10
Positive values: approximately 4.9 x 10 to the -324th power to the 308th power of approximately 1.7 x 10
Workbookworkbook
Worksheetworksheet
RangeCell Range

The usage of Workbook, Worksheet, and Range is slightly different and will be explained separately.

How to use the variable type "Workbook

How to use each variable type

Usage of "string" (type of string)

Use "string" to store strings.

Sub Show name()
    Dim name As String
    name = "Tanaka".
    MsgBox name
End Sub

When I run this code

was displayed.

Dim name As String" to declare that "name" is a string, and

The string "Tanaka" is stored in "name" with "name = "Tanaka".

How to use "long" (type of integer)

Use "long" to store integers.

Sub Display Age()
    Dim age As Long
    age = 30
    MsgBox age
End Sub

When I run this code

was displayed.

Dim age As Long" to declare that "age" is an integer, and

The integer "30" is stored in "age" with "age = 30".

How to use "Double" (few types)

If you want to store a small number, use "Double".

Sub Display of quarters()
    Dim quarter As Double
    quarter = 1 / 4
    MsgBox quarter
End Sub

When I run this code

was displayed.

Dim quarter As Double" to declare that "quarter" is a minority, and

quarter = 1 / 4" stores "1/4 calculation result" in "quarter".

Variable types that need to be used differently for different purposes

As we continue to learn more about vba.

Variable Typesrange of data that can be stored
DateDate
January 1, 100 to December 31, 9999
BooleanCall it Boolean.
True or False is entered.

Types of variables that do not need to be used

Other variable types I rarely use

Variable Typesrange of data that can be stored
IntegerInteger (32,768 to 32,767)
Same integer as long, but much smaller range.
ByteInteger (0-255)
Same integer as long, but much smaller range.
SingleSame small number as Double, but smaller range.
Negative values: -3.4 x 10 to the 38th power to the -1.4 x 10 -45th power
Positive values: 1.4 x 10 to the -45th power to the 38th power of 3.4 x 10
VariantNo distinction between numbers and strings, as anything can be entered.
ObjectWorkbook, Worksheet, Range, etc. are all included, but these
Better to use differently for Workbook, Worksheet and Range.

Comment

Copied title and URL