Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to sheet using codename

I get a "type mismatch" error in this code:

With Worksheets(Sheet1)   '* Error here 
   'my code here
End With

My sheet's CodeName is 'sheet1'.

Can someone please help me remove the error?

like image 967
user007 Avatar asked Jan 05 '17 05:01

user007


People also ask

What is the sheet code name?

A sheet name code is a friendly and meaningful name that you can assign to the sheets in your workbook. This is especially useful if you have more than one worksheet because the names can be different. Creating a sheet name in excel is very important to keep track of your data.

Where is the sheet name code in Excel?

Sheet name code Excel formula Step 1: Type “CELL(“filename”,A1)”. The cell function is used to get the full filename and path. This function returns the filename of . xls workbook, including the sheet name.


2 Answers

1) Refer to sheet by Index:

With Worksheets(1) 
    '<stuff here>
End With

The `Index' is dependent on the "order of sheets in the workbook". If you shuffle your sheets order, this may not refer to the same sheet any more!

2) Refer to sheet by Name:

With Worksheets("Your Sheet Name") 
    '<stuff here>
End With

This is the .Name property of a worksheet, and is the name visible in the Excel worksheet tab and in brackets in the VBA Project Explorer.

3) Refer to sheet by CodeName:

You suggested you actually wanted to use the .CodeName property of a worksheet. This cannot be reference within brackets like the above two examples, but does exist contrary to some answers above! It is assigned automatically to a sheet on creation, and is "Sheet" then the next unused number in the previously created CodeNames.

The advantage of using CodeName is that it doesn't depend on the sheet order (unlike the Index) and it doesn't change if a user changes the Name simply by renaming the sheet in Excel.

The disadvantage is the code can be more convoluted or ambiguous. Since CodeName is read-only [1] this cannot be improved, but does ensure the above advantages! See the referenced documentation for more details.

First way of using it: directly...

With Sheet1
    '<stuff here>
End With

Second way of using it: indirectly, may offer more clarity or flexibility, shows how to use the CodeName property of a worksheet...

By looping over sheets and reading the CodeName property, you can first find either the Index or Name property of your desired sheet. Then your can use this to reference the sheet.

Dim sh as WorkSheet
Dim shName as String
Dim shIndex as Long

' Cycle through all sheets until sheet with desired CodeName is found
For Each sh in ThisWorkbook.WorkSheets        
    ' Say the codename you're interested in is Sheet1
    If sh.CodeName = "Sheet1" Then            
        ' - If you didn't want to refer to this sheet later, 
        '   you could do all necessary operations here, and never use shName
        '   or the later With block.
        ' - If you do want to refer to this sheet later,
        '   you will need to store either the Name or Index (below shows both)

        ' Store sheet's Name
        shName = sh.Name            
        ' Store sheet's Index
        shIndex = sh.Index           
    End If
Next sh 

' Check if match was found, do stuff as before if it was!
If shName = "" Then
    MsgBox "Could not find matching codename"
Else 
    ' Equally to the next line, could use Worksheets(shIndex)
    With Worksheets(shName)
        '<stuff here>
    End With
End If

[1] https://msdn.microsoft.com/en-us/library/office/ff837552.aspx

like image 130
Wolfie Avatar answered Sep 23 '22 10:09

Wolfie


There are 3 different properties which could be used to refer to a worksheet:

  • .Name as Worksheets("SomeNameHere") in Worksheets("SomeNameHere").Range("A1")
  • .Index as Worksheets(2) in Worksheets(2).Range("A1")
  • .CodeName as Sheet3 in Sheet3.Range("A1")

To see the difference, run the code below and take a look at the immediate window Ctrl+G:

Sub TestMe()
    Dim wks As Worksheet
    For Each wks In ThisWorkbook.Worksheets
        Debug.Print wks.Name
        Debug.Print wks.Index
        Debug.Print wks.CodeName
        Debug.Print "-----------------------"
    Next wks
End Sub

If the Name and the CodeName of the worksheet are not changed, they would be the same.

  • CodeName:

enter image description here

  • Name:

enter image description here

like image 35
Vityata Avatar answered Sep 24 '22 10:09

Vityata