VBC09333 — Variable required
Severity: Error · Family: VBC (static semantics / abstract syntax tree) ·
Source metadata: VBCompileErrorInfo
An expression that is not a variable is where a statement requires a variable: a value is to be assigned into it.
MS-VBA reports the same error: Variable required - can't assign to this expression.
This code is raised for the statements whose operands the specification says "MUST be classified as a variable" (MS-VBAL §5.6.1):
MidStatementStaticSemantics and
FileStatementStaticSemantics. It is raised as well for
the target of a Let, Set, LSet or RSet statement, the control variable of a For or For Each loop and the target of a
ReDim statement, which can't be a constant nor a function of the library (MS-VBAL §5.4.3.8, §5.4.3.9, §5.6.16.6,
§5.4.3.3) - though the target of a Let statement may be a function of the library whose declared type is a class, Object or
Variant (MS-VBAL §5.4.3.8) - and for an element of an Erase statement, which can't be a constant (MS-VBAL §5.4.3.4).
How it is raised
| Situation | Example | Resolution |
|---|---|---|
The target of a Mid statement is not a variable. |
Mid(Left, 1, 1) = "x" |
Assign into a variable that holds the string. |
The target of a Line Input #, an Input # or a Get is not a variable. |
Line Input #1, "x" |
Read into a variable. |
| The expression is a constant. | Input #1, Limit, for Const Limit = 10 |
Read into a variable. |
| The target of an assignment is a constant. | vbCrLf = 5, Limit = 5, VbDayOfWeek.vbSunday = 1 |
Assign to a variable. |
The target of an assignment is a function of the library - unless the statement is a Let and the function's declared type is a class, Object or Variant. |
Left$ = "x", Set Left = Nothing, LSet Err = "x" |
Assign to a variable. |
The control variable of a For or For Each loop is a constant. |
For vbSunday = 1 To 7 |
Count with a variable. |
The target of a ReDim statement, or an element of an Erase statement, is a constant. |
Erase vbCrLf, ReDim Limit(1) |
Erase or redimension an array variable. |
The argument of Len or LenB is neither a String (or a Variant) nor a variable (RD-VBAL §6.1.2.11.1.22): a number, a Boolean or a date that is a literal, a constant, the result of an operator, a function or a property, or an object whose default member returns neither a String nor a Variant. |
Len(5), Len(FnLong()), Len(Total) for a Total object whose default member returns a Long |
To count characters, convert it: Len(CStr(5)). To measure the size of its type, assign it to a variable and measure the variable. |
Notes
Only an expression that is certainly not a variable is reported: a literal, the result of an operator, a constant - by its name, qualified by its project or its
module, or qualified by the Enum of the library that declares it - and a function of the library. Which of the others - a member access, an index - is a variable
depends on what it resolves to, and is not judged here. A name written with a type-declaration character is what its identifier names (MS-VBAL §5.6.10): after
Dim Left$, Left$ = "x" is not reported. A function of the workspace assigned inside its own body is not reported, since there its name is its result, nor is a
property. Nor is a function of the library whose declared type is a class, Object or Variant, as the target of a Let statement, since MS-VBAL
§5.4.3.8 admits it: it assigns an object's default member (Err = 5), and has the statement raise error 450 when it runs for a Variant (Left = "x").
An element of an Erase statement may be a function (MS-VBAL §5.4.3.4), so only a constant is reported there.
Note
Not implemented. Some targets MS-VBAL §5.4.3.8 makes invalid are not reported yet: a function of the workspace or a Declared function assigned
from outside its own body (F = 6 in another procedure), when its declared type is not a class, Object or Variant. It does not run: it fails in the
host rather than raising a run-time error. Nor is an unqualified ReDim target that the declaration pass does not see - a name only the library
or another module declares (ReDim vbCrLf(1), ReDim Left(1)) - since it becomes an implicit declaration of a local array; qualified
(ReDim Strings.Left(1)), it is reported. See RD-VBAL §5.4.3.3 ReDim Statement.
See also
- RD-VBAL §5.4.3.8 Let Statement
- RD-VBAL §5.4.3.5 Mid Statement
- RD-VBAL §5.4.5 File Statements
- RD-VBAL §5.6.16.6 Bound Variable Expressions
- RD-VBAL §2.6.2 Semantic Compilation Errors
⏮️ VBC09332 Exit Sub not allowed in Function or Property | ⏭️ VBC09334 Access not valid for the file mode
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca