【问题标题】:Worksheet names in VBEVBE 中的工作表名称
【发布时间】:2016-12-23 06:06:17
【问题描述】:

我注意到我的工作簿对于 VBE 中的每个组件都有两个不同的名称。 name1 和 name2 有什么区别? 我应该参考哪一个,这样我才能确定我的宏可以工作?

【问题讨论】:

标签: vba excel vbe


【解决方案1】:

Control 是工作表的代码名称,而Plan 1 是工作表的选项卡名称。后者可以由用户轻松更改,因此如果可以的话,使用代号会更安全 - 例如,参考:

control.range("A1:A10")

而不是:

sheets("Plan 1").Range("A1:A10")

请注意,您不能使用工作表代号来引用工作簿中包含代码的工作表以外的工作表,除非您设置对该工作簿项目的引用,或使用a function that loops through each sheet in the other workbook testing the codename property of each

【讨论】:

  • 关于在同一个项目中引用的更多信息。
【解决方案2】:

“Plan1”是选项卡名称,显示在工作表底部的选项卡上。

“Control”是代号,可在 VBA 中用于直接引用该特定工作表对象。

Sheets("Plan1").Cells(1, 1).ValueControl.Cells(1, 1).Value 将产生相同的输出。

【讨论】:

    【解决方案3】:

    VBE 中的每个文档类型的 vbComponent 都有一个Name 和一个CodeName

    • Name 是在 Excel 用户界面的工作表选项卡上可见的名称。
    • CodeName 是可以在 VBA 中引用的工作表对象的名称。

    例子:

    Sub Names()
    
      Debug.Print Control.Name              'Prints "Plan 1"
      Debug.Print Control.CodeName          'Prints "Control"
    
      'This approach uses the sheet name in a call to `Sheets`,
      ' which will break if a user changes the name of the sheet
      'The sheets collection returns an object, so you don't get
      ' Intellisense, or compile-time error checking
      Debug.Print Sheets("Plan 1").Name     'Prints "Plan 1"
      Debug.Print Sheets("Plan 1").CodeName 'Prints "Control"
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多