What does it mean if a number is written in square brackets like [5] What does it mean if a number is written in square brackets like [5] vba vba

What does it mean if a number is written in square brackets like [5]


Square brackets in VBA/VB6 are used for accessing "foreign identifiers", i.e. identifiers that would otherwise not be legal. For example:

Public Enum Foo    Some    Thing    [Some Thing] ' please don't do thisEnd Enum

And hidden members whose name begins with an underscore:

Public Property Get NewEnum() As IUnknown    Set NewEnum = myCollection.[_NewEnum]End Property

However in the context of the code in this question, the square brackets are, as Scott indicated, essentially shorthand notation for [_Global].Evaluate, which ultimately resolves to Application.Evaluate... assuming we're not in a Worksheet module's code-behind, in which case it's shorthand for Worksheet.Evaluate - and both return a Variant, which means any chained member calls are blind, late-bound calls resolved at run-time: Option Explicit can't save you from a typo.

That's why Rubberduck (an open-source VBIDE add-in project I manage / contribute to) resolves them as "runtime expressions":

Rubberduck's context-sensitive toolbar showing 'A1' as a 'runtime expression'

In other words this:

Range("E16").Value = Mid(line, 49, [6])

Could just as well be written like this:

[E16] = Mid(line, [49], [6])

...which is arguably terrible code with a ton of redundant implicit operations going on.

There is never, NEVER any reason whatsoever to square-bracket an integer literal: it's nothing more than a rather roundabout way to turn an Integer literal into a Double (since worksheet numeric values are Variant/Double):

Debug.Print TypeName(42)IntegerDebug.Print TypeName([42])Double

Which is much better off done with an explicit conversion:

Debug.Print TypeName(CDbl(42))Double

...or even with a (gasp) type hint:

Debug.Print TypeName(42#)Double