• VBA基础十:With语句,省略相同的前缀


    前边如果用到了

    Sub with嵌套1()
    Sheet1.Range("a1").Value = "我是谁"
    Sheet1.Range("a1").Parent.Name = "Hello World"
    Sheet1.Range("a1").Font.Size = 20
    Sheet1.Range("a1").Font.Bold = True
    End Sub

    后边再用到,就可以省去前边的头  Sheet1.Range("a1"),例如下:

    Sub with嵌套2()
       With Range("a1")
          .Value = "我是谁"
          .Parent.Name = "Hello World"
       With .Font
          .Size = 20
          .Bold = True
       End With
    End With
    End Sub

    还可以有:

    With object
    [statements]
    End With

    注意 当程序一旦进入 With 块,object 就不能改变。因此不能用一个 With 语句来设置多个不同的对象。
    可以将一个 With 块放在另一个之中,而产生嵌套的 With 语句。但是,由于外层 With 块成员会在内层的 With 块中被屏蔽住,所以必须在内层的 With 块中,使用完整的对象引用来指出在外层的 With 块中的对象成员。
    重点 一般来说,建议您不要跳入或跳出 With 块。如果在 With 块中的语句被执行,但是 With 或 End With 语句并没有执行,则一个包含对该对象引用的临时变量将保留在内存中,直到您退出该过程。

  • 相关阅读:
    error occurred during initialization of vm
    Service Discovery protocol(SDP)
    nRF51822EK_PRO
    Binder
    android samsung note3  device not found
    BLE pairing vs. bonding
    remote link Centos6.6 Horrible Slow
    depmod -a
    start and end call use itelephony and how to pick up a call
    WebService
  • 原文地址:https://www.cnblogs.com/yuanscn/p/13328163.html
Copyright © 2020-2023  润新知