Name limit - VBA


Name 255 character limit


In VBA, names of variables, constants, arrays, procedures, and other items have a limit of 255 characters. This module demonstrates that fact. Code 1 line 5 declares a Private variable with a name comprising the character sequence A to z; 9 times (26 characters x 9 = 234 characters) and A to u (26 characters - 5 = 21) for a total of 255 characters.

The length of the string {A, b, c, …, u} is verified in line 13.



Code 1: Dim 255 character variable name with Module scope
Option Explicit

' Declare 255 character name
' One more char returns Identifier too long
Dim AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstu

Sub LenLongVar()
    Dim int_
    Dim LenVar As Integer

    LenVar = Len("Abc")
    ' Returns 3
    LenVar = Len("AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstu")
    ' Returns 255

End Sub												

Appending one more character to the string in line 5 (ie. 256 characters) returns Compiler error: Identifier too long