API reference · Others

Get_WS_Codename

Looks up a worksheet in ThisWorkbook by its VBA CodeName.

FunctionOthers→WorksheetSource L27

Signature#

VBA
Function Get_WS_Codename(ByVal sName As String) As Worksheet

Parameters#

ParameterVBA typeDefaultDescription
sNameStringByValRequiredWorksheet VBA CodeName to match. This is not necessarily the visible sheet-tab Name.

Returns#

Worksheet object for the first matching worksheet in ThisWorkbook, or Nothing when no match is found.

Example#

SEE THE TRANSFORMATIONLook up the first worksheet by CodeName
Before
Illustrative workbookValues / state

No array dimensions

ItemValue
First worksheet CodeName“Feuil1”
Visible sheet name“Transactions”
After
Returned Worksheet objectValues / state

No array dimensions

ItemValue
CodeName“Feuil1”
Name“Transactions”

Sheet names here are illustrative. The code uses the actual first worksheet’s CodeName. This returns an object reference, not a new array or a renamed sheet.

Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.

VBA
Option Explicit

Public Sub ExampleOthersGetWorksheet()
    Dim utility As New Others
    Dim target As Worksheet
    If ThisWorkbook.Worksheets.Count = 0 Then Exit Sub

    Set target = utility.Get_WS_Codename(ThisWorkbook.Worksheets(1).CodeName)
    If Not target Is Nothing Then Debug.Print target.Name
End Sub

Behavior and edge cases#

Performance#

O(w) CodeName comparisons for w worksheets in ThisWorkbook, with O(1) explicit auxiliary state.

View implementation
VBA
Function Get_WS_Codename(ByVal sName As String) As Worksheet
    Dim lCount As Long

    For lCount = 1 To ThisWorkbook.Worksheets.Count
        If sName = ThisWorkbook.Worksheets(lCount).CodeName Then
            Set Get_WS_Codename = ThisWorkbook.Worksheets(lCount)
            Exit For
        End If
    Next lCount
End Function