Table of Contents

VBC09368 — Invalid use of New keyword

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

A variable is declared As New of something New cannot create an object of.

As New makes the variable create an object of its class the first time it is referred to. That needs a class, and one the project can create.

MS-VBA reports the same error, with the same message (recorded in Excel), for a type that is no class and for a WithEvents variable. For a class of another project that cannot be created, MS-VBA compiles the declaration, and raises run-time error -2147221164 Class not registered when the variable is first referred to; RD-VBA reports it before the code runs.

This code is raised where the module's declarations are checked.

How it is raised

Situation Example Resolution
The type is a user-defined type or an enumeration. Dim p As New TPoint, Dim c As New VbCompareMethod Declare the variable As TPoint: a record needs no New.
The variable is declared WithEvents. Private WithEvents Book As New Workbook Declare it without New, and Set it to the object whose events it handles.
The class belongs to a library, and cannot be created. Dim wb As New Workbook, Dim e As New ErrObject Get the object from the library: Set wb = Workbooks.Add.

A type that is no class at all - As New Long, As New String, As New Variant, As New Object - is a syntax error, VBC00001, as it is in MS-VBA.

See also


⏮️ VBC09367 Invalid use of AddressOf operator | ⏭️ VBC09369 Can't change data types of array elements


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