Most feasible way to work with Worksheet names in VBA code


1.       When you look in your VBA project screen it will represent a worksheet name  “Sheet1” (which is the default name when you open a workbook) as follows in the VBA project explorer:
Microsoft Excel Objects
Sheet1(Sheet1)                ßThe name in brackets is the name as you see it in the workbook and is the worksheet name.
                                               ß The name on the left is the code name.
The worksheet name can be changed in the workbook or in the code module by selecting the sheet object and then changing the Name property in the properties window.
The code name can only be changed in the properties window under the (Name) property.
2.       To refer to the worksheet name in code we refer to it in the code module as follows:
ThisWorkbook.Worksheets(“Sheet1”).Range(”A1”) = 66
3.       To refer to the worksheet by using its code name you can use the following code and it can be referred to directly.
Sheet1.Range(“A1”) = 66
4.       The benefit of using the code name for  the worksheet instead of the worksheet name is that when the user change the name of the worksheet the code will still work. This is because the code refers to the code name for the worksheet and not to the worksheet name.
To use the code name of the worksheet the worksheet must be in the same workbook than the code.

Comments

Popular posts from this blog

VBA Developer - Digitally sign your macro project

Distribute your VBA project to other Microsoft Users

How far can you stretch the Excel LET function?