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, or New names one.

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. A class of a library can be created when the library says so.

MS-VBA reports the same error, with the same message (recorded in Excel), in each situation below.

This code is raised where the module's declarations are checked, and where a New expression is.

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 type is an interface of a library, or a class the library does not say can be created. Dim f As New Font, Set r = New Range, Set c = New Office.CommandBars Get the object from the library: Set r = Range("A1"), Set c = Application.CommandBars.

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.

A class the library says can be created compiles, whether or not an object of it can be made when the program runs. New ErrObject compiles, and fails whenever it runs: see RDC11028.

See also


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