Identifying a worksheet other than by its name

前端 未结 2 876
清歌不尽
清歌不尽 2020-12-07 03:34

A worksheet can be identified by its name such as in this example:

Dim mysheet As Worksheet
Set mysheet = ThisWorkbook.Sheets(\"My Sheetname\")
2条回答
  •  太阳男子
    2020-12-07 04:00

    This is a very good article that explains that it is better to use the SheetID (also called codename) instead of the Sheet Name.

    Quoting:

    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"
    

    and then

    Revenue_Actuals.Range("C2").value = 10
    

    works fine.

    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.

    Finally, this article lists many ways that a sheet or workbook can be referenced.

    I hope this helps!

提交回复
热议问题