Table of Contents

VBC09307 — Expected array

Severity: Error  ·  Family: VBC (static semantics / abstract syntax tree)  ·  Source metadata: VBCompileErrorInfo

LBound or UBound is asked the bounds of something that can never be an array.

LBound and UBound give the bounds of an array. A variable declared as a number, a String, a Boolean or a Date never holds one, and neither does an object, a variable of a class, a record of a user-defined type, a project, a module or an enumeration, so asking its bounds can only fail: the code is refused before it runs. What the bounds are asked of is written as a name, a member or a call: a literal, an operator's result or anything in parentheses (UBound(5), UBound(n + 1), UBound((a))) is VBC00001 Syntax error instead.

MS-VBA reports the same error: Expected array.

This code is raised by ExpressionStaticSemanticsEvaluator, at the expression whose bounds are asked.

How it is raised

Situation Example Resolution
The bounds of a variable of an intrinsic type. Dim n As Long … Debug.Print UBound(n) Declare the variable as an array (Dim n() As Long), or as a Variant that holds one.
The bounds of an object or a record. Dim c As Collection … UBound(c), Dim o As Object … UBound(o), UBound(q) of a Type Ask the bounds of the array the object or the record holds: UBound(q.Items). A Collection's items are counted by c.Count.
The bounds of a project, a module or an enumeration. UBound(Strings), UBound(VBA), UBound(Color) Ask the bounds of an array.
A ReDim of a name that is a variable of another type, a constant or a member of an enumeration, of any module or library. ReDim Count(1) of another module's Public Count As Long, ReDim vbCrLf(1), ReDim Red(1) Re-dimension an array: a ReDim re-dimensions what its name matches, and declares a new array only when the name matches nothing.
An Erase of a function, or of a property of a module, that does not return a Variant. Erase Total of a Function Total() As Long, Erase Err, Erase Timer Erase a variable of your own that holds the array.

Notes

What MS-VBA does, recorded in Excel:

  • A Variant can hold an array, and compiles: when it holds something else, LBound and UBound raise error 13, Type mismatch, when the code runs.
  • A dimension that does not exist (UBound(a, 3) of a two-dimensional array, or UBound(a, 0)) is not known until the code runs either, and raises error 9, Subscript out of range.

See also


⏮️ VBC09306 Invalid ReDim | ⏭️ VBC09309 Label not defined


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca