Skip to main content

With

Executes a series of statements on a single object.

Synopsis

With object
   statements
End With

Arguments

The With statement requires the following arguments:

object An expression that resolves to an object reference. object may be a function call that returns an object reference, or a subscripted variable that contains an object reference.
statements One or more statements to be executed on the object.

Description

The With statement allows you to perform a series of statements on a specified object without requalifying the name of the object. For example, to change a number of different properties on a single object, place the property assignment statements within the With block code, referring to the object once instead of referring to it with each property assignment.

The object object reference is evaluated upon entering the With block, and is not reevaluated within the With block. Therefore, you cannot use a single With statement to affect a number of different objects. Changing the object variable value within the With block is permitted, but does not change which object is used for anonymous references within the With block.

While property manipulation is an important aspect of With functionality, it is not the only use. Any legal code can be used within a With block.

You can nest With statements by placing one With block within another. However, because members of outer With blocks are masked within the inner With blocks, you must provide a fully qualified object reference in an inner With block to any member of an object in an outer With block.

A Goto statement cannot be used to enter the body of a With block, or into a nested inner With block. You can, however, issue a Goto within a With block to a label within that block, or to an outer With block label, or to a label outside the With block.

Examples

The following example illustrates use of the With statement to assign values to several properties of the same object.

With myPerson
   .City = "Cambridge"
   .State = "MA"
   .Street = "One Memorial Drive"
End With
FeedbackOpens in a new tab