2015-03-25 67 views
2

我注意到我的工作簿对于VBE中的每个组件都有两个不同的名称。 name1和name2有什么区别? 我应该引用哪一个,所以我肯定我的宏将起作用?VBE中的工作表名称

enter image description here

+0

可能重复http://stackoverflow.com/questions/2649844/excel-tab-sheet-names- vs-visual-basic-sheet-names) – 2015-03-25 11:48:37

+0

也相关:http://stackoverflow.com/questions/27169070/identifying-a-worksheet-other-than-by-its-name和其他更多:http:/ /stackoverflow.com/search?q=sheet+name+codename – 2015-03-25 11:48:59

回答

5

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来引用除包含代码之外的工作簿中的工作表。

+0

对于在同一个项目中引用更多信息的好消息。 – 2015-03-25 11:19:01

2

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

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

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

0

在VBE每个文档型的VBComponent具有NameCodeName

  • 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 
[EXCEL选项卡表的名称与Visual Basic中表名称](的