Login / Register ID | EN
This page has no official English version. It was translated automatically and may contain errors. Read the original in Indonesian →
Tipe Data Variabel VBA Excel: Rentang Nilai, Ukuran, dan Overflow
Foto: Pexels
Office IT

Variable Data Types in VBA Excel: Value Range, Size, and Overflow

Variables in VBA hold values while the macro is running. The type you write after As determines two things: how large the number can be, and how many bytes of memory are used. Choosing incorrectly can cause a macro that was running smoothly yesterday to stop with an Overflow message when the data increases.

Table of commonly used data types

Type Size Value Range
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,647
Double 8 bytes up to about 1.79769E308, both positive and negative
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Boolean 2 bytes True or False
Date 8 bytes January 1, 100 to December 31, 9999
String (variable length) 10 bytes + text length 0 to about 2 billion characters
Object 4 bytes reference to any object
Variant (containing a number) 16 bytes any number up to the Double range

The numbers in this table are written with thousand separators and decimal commas. In the VBA editor, decimals are still written with a dot.

Integer is not enough for row numbers

An Excel worksheet now has 1,048,576 rows by 16,384 columns. The upper limit of Integer is only 32,767. A row counter declared As Integer will get stuck at row 32,768 with error 6, Overflow. For row numbers, use Long. The difference is only 2 bytes of memory.

The second trap is more subtle. Microsoft documentation gives an example x = 2000 * 365 which still results in Overflow even though x is already Long. The two numbers on the right are treated as Integer, and their product (730,000) exceeds the Integer limit before it can be stored in x. The solution is to make one of the numbers Long first:

Dim x As Long
x = CLng(2000) * 365

One Dim line, two different types

Dim jumlah, total As Long

Many people read this line as two Long variables. In reality, only total is Long. jumlah is not given a type, so it automatically becomes Variant. Each variable needs its own As:

Dim jumlah As Long, total As Long

Variant is indeed flexible, but it takes 16 bytes for a number, and the wrong type is only discovered when the macro is already running.

Initial values before being assigned

Once declared, numeric variables have a value of 0, String variables have a value of an empty string, Variant variables have a value of Empty, and object variables have a value of Nothing. As a result, a macro that forgets to assign a variable does not always stop. It continues running with a zero value, leading to incorrect results without warning.

Object variables assigned via Set

Variables like Worksheet, Range, or Workbook only store references. The line Dim ws As Worksheet does not point to any sheet yet. The reference is only filled via Set, and that variable must be assigned before use:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Data")

Force declaration with Option Explicit

Write Option Explicit at the very top of the module, before the first procedure. With this line, any undeclared variable names are immediately rejected during compilation. Typos like totl are caught before the macro runs, rather than silently becoming a new empty Variant.

Checklist before running the macro

  1. Option Explicit is at the top of every module.
  2. Row and column counters use Long, not Integer.
  3. Each variable in a single Dim line has its own As.
  4. Multiplication of numbers that could exceed 32,767 is given CLng on one of the numbers.
  5. Object variables are assigned with Set before use.

If you prefer to see the basic explanation directly, there is a clip: what is a variable in VBA Excel.

Sources