工作表可以通过其名称来标识,例如:
A worksheet can be identified by its name such as in this example:
Dim mysheet As Worksheet Set mysheet = ThisWorkbook.Sheets("My Sheetname")现在我想知道工作表是否可以被识别其他比起它的名字,例如通过一些唯一的id或某些属性或任何东西。
Now I wonder if a worksheet can be identified other than by its name, for example by some unique id or some property or whatever.
我的问题是:
参考上面的代码片段:如果用户更改了工作表的名称(例如从我的工作表到您的工作表),则该代码片段显然将无法工作。
Referring to the code snippet above: if a user changes the name of the worksheet (e.g. from "My Sheetname" to "Your Sheetname"), the snippet obviously won't work anymore.
推荐答案这是一个非常好的文章,解释说最好使用 SheetID (也称为codename),而不是
This is a very good article that explains that it is better to use the SheetID (also called codename) instead of the Sheet Name.
报价:
代号p roperty是Excel用于标识每个表的内部名称。不同于Worksheet.Name属性,代码名称保持相同,无论工作表名称或工作表顺序。
The Codename property is the internal name that Excel uses to identify each sheet. Unlike the Worksheet.Name property, the Codename remains the same regardless of sheet name, or sheet order.
代码名称也可以更改,以便它更具描述性:
The code name can also be changed so that it is more descriptive:
ThisWorkbook.VBProject.VBComponents("Sheet1").Name = "Revenue_Actuals"然后
Revenue_Actuals.Range("C2").value = 10
使用代号(例如 Sheet1.Range(C1)。value )是一个好主意,然而,如上所述,在运行时更改代码名称不被认为是一些开发人员的好习惯(参见例如对上面链接的文章的评论)。
Using codenames (such as Sheet1.Range("C1").value) is a good idea, however changing code names at runtime as above is not considered good practice by some developers (see for example, comments on the article of the link above).
使用表单索引是另一种方式,但我个人更喜欢代码名称。
Using the sheet index is another way to go, but I personally prefer the code name.
最后, this a
Finally, this article lists many ways that a sheet or workbook can be referenced.
希望这有帮助!
更多推荐
识别除了其名称之外的工作表
发布评论