With 语句
语法
Withobject [ statements ] End With
“With”语句语法包含以下部分:
Part | 说明 |
---|---|
object | 必填。 对象或用户定义类型的名称。 |
statements | 可选。 将对对象 执行的一个或多个语句。 |
备注
The With statement allows you to perform a series of statements on a specified object without requalifying the name of the object. 例如,若要更改单个对象上的多个不同 属性 ,请将属性赋值语句置于 With 控件结构中,引用对象一次,而不是使用每个属性赋值引用该对象。
The following example illustrates use of the With statement to assign values to several properties of the same object.
With MyLabel
.Height = 2000
.Width = 2000
.Caption = "This is MyLabel"
End With
注意
输入“With”块后将无法更改对象。 As a result, you can't use a single With statement to affect a number of different objects.
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.
注意
In general, it's recommended that you don't jump into or out of With blocks. 如果执行了“With块中的语句,但未执行“With”或“End With”语句,则内存中将保留包含对对象的引用的临时变量,直到您退出过程。
示例
This example uses the With statement to execute a series of statements on a single object. The object and its properties are generic names used for illustration purposes only.
With MyObject
.Height = 100 ' Same as MyObject.Height = 100.
.Caption = "Hello World" ' Same as MyObject.Caption = "Hello World".
With .Font
.Color = Red ' Same as MyObject.Font.Color = Red.
.Bold = True ' Same as MyObject.Font.Bold = True.
End With
End With
另请参阅
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。