2

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

在此处输入图像描述

4

3 回答 3

5

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

control.range("A1:A10")

而不是:

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

请注意,您不能使用工作表代号来引用包含代码的工作簿以外的工作表中的工作表,除非您设置对该工作簿项目的引用,或者使用循环遍历另一个工作簿中的每个工作表的函数来测试代号属性每个.

于 2015-03-25T11:14:40.997 回答
2

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

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

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

于 2015-03-25T11:15:49.027 回答
0

VBE 中的每个文档类型 vbComponent 都有 aName和 a CodeName

  • Name是在 Excel UI 的工作表选项卡上可见的名称。
  • 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
于 2016-12-22T22:06:17.750 回答