Skip to Content
VB & VBA in a Nutshell: The Language
book

VB & VBA in a Nutshell: The Language

by Paul Lomax
October 1998
Intermediate to advanced
656 pages
16h 10m
English
O'Reilly Media, Inc.
Content preview from VB & VBA in a Nutshell: The Language
Public Statement

Named Arguments

No

Syntax

Public [WithEvents] varname[([subscripts])] _
   [As [New]  type] [, [WithEvents] _
   varname[([subscripts])] [As [New] type]] . . .

WithEvents

Use: Optional

Type: Keyword

A keyword that denotes the object variable varname can respond to events triggered from within the object to which it refers.

varname

Use: Required

Data Type: Any

The name of the variable, which must follow Visual Basic naming conventions.

subscripts

Use: Optional

Denotes varname as an array and specifies the dimensions and number of elements of the array.

New

Use: Optional

Type: Keyword

Used to automatically create an instance of the object referred to by the object variable varname.

type

Use: Optional

Data type of the variable varname.

Description

Used at module level to declare a public variable and allocate the relevant storage space in memory. A Public variable has both project-level scope—that is, it can be used by all procedures in all modules in the project—and, when used in a class module, it can have scope outside the project.

Rules at a Glance

  • The behavior of a Public variable depends on where it's declared, as the following table shows:

    Variable Declared in... Scope
    Any procedure Illegal; generates a compile-time error.
    Code module declarations section Variable is available to all modules within the project.
    Class module declarations section Variable is available as a property of the class to all modules within the project, and to all other projects referencing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Absolute Beginner's Guide to VBA

Absolute Beginner's Guide to VBA

Paul McFedries
Visual Basic 2015 Unleashed

Visual Basic 2015 Unleashed

Alessandro Del Sole

Publisher Resources

ISBN: 1565923588Catalog PageErrata