Table of Contents

VBC09379 — For Each may only iterate over a collection object or an array

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

A For Each loop iterates a value that has no elements.

For Each goes through the elements of an array, or the items of a collection object such as a Collection. A number, a String, a Boolean, a Date or a record of a user-defined type has none, and neither does a project, a module or an enumeration.

MS-VBA reports the same error, with the same message (recorded in Excel). What follows In is a name, a member or a call: a literal, an operator's result, or anything in parentheses is VBC00001 Syntax error instead (For Each v In 5, For Each v In (a)).

How it is raised

Situation Example Resolution
A variable of an intrinsic type, or a record. Dim n As Long … For Each v In n Iterate an array or a collection. To go through the characters of a String, loop on Mid$(s, i, 1) with For i = 1 To Len(s).
A function that returns no array. For Each v In Count(), of a Function Count() As Long Iterate what the function returns an array or a collection of.
A project, a module or an enumeration. For Each v In Strings, For Each v In Color Iterate an array or a collection.

A Variant is iterated as what it holds when the loop runs: one that holds no array and no object is run-time error 92.

See also


⏮️ VBC09378 With object must be user-defined type, Object, or Variant | ⏭️ VBR-0001 Application-defined or object-defined error


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