VBA-with语句

声明:笔记来源于我要自学网-《Excel VBA基础教程》-授课讲师:曾贤志


with语句,当对某个对象执行一系列的语句时,不用重复指出对象的名称。

Sub with语句1()
    a = Range("a1").Address
    b = Range("a1").Parent.Name
    Range("a1") = "1234"
End Sub
Sub with语句2()    '简写代码
With Range("a1")
    a = .Address
    b = .Parent.Name
    .Value = "1234"
End With
End Sub
Sub with嵌套1()
Range("a1").Value = "我是谁"
Range("a1").Parent.Name = "Hello World"
Range("a1").Font.Size = 20
Range("a1").Font.Bold = True
End Sub
Sub with嵌套2()
With Range("a1")
    .Value = "我是谁"
    .Parent.Name = "Hello World"
    With .Font
        .Size = 20
        .Bold = True
    End With
End With
End Sub

 

你可能感兴趣的:(Visual,Basic宏语言)