Refering to codename of worksheet

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,913
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
IF I know my workbook has a worksheet called Apples, I can refer to it as follows:

Code:
With wb.Worksheets("Apples")

    ' Do something

End With

but I DON'T know for certain there's a worksheet called Apples because the user might change it.


Instead I DO know there's a worksheet with a codename of wksApples.


So how can incorporate this into my code?


This doesn't work:


Code:
With wb.Worksheets(wksApples.Codename)


    ' Do something


End With

I don't want to have to do this:


Code:
    Dim ws As Worksheet


    For Each ws In wb.Worksheets


        If ws.CodeName = "wksApples" Then


            Exit For


        End If


    Next ws


    With ws


        ' Do something


    End With


Thanks
 
Last edited:
You don't need to loop to find a sheet with a given code name


Code:
Sub test()
    MsgBox WorksheetCodeNamed("wksApples").Range("A1").Value
End Sub

Function WorksheetCodeNamed(wsCodeName As String, Optional wb As Workbook)
    If wb Is Nothing Then Set wb = ActiveWorkbook
    
    With wb
        Set WorksheetCodeNamed = .Sheets(.VBProject.VBComponents(wsCodeName).Properties("index").Value)
    End With
End Function

Thanks Mike,

What's the significance of "index"?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Speaking of which, any idea why this is the case? It would make some of our lives a whole lot easier if we could refer to the codename of a worksheet in a specific WB.
No idea why, but you can do it if you set a reference to the other workbook. Although I think that means the other workbook will automatically open, whenever you open the "Master"

You don't need to loop.
Agree, but that means you need to enable "Trust access to the VBA Project".
 
Upvote 0
Thanks Mike,

What's the significance of "index"?

VBComponent("codename").Property("index").Value returns the index of the sheet (i.e. position from the left of the tabs)
VBComponent("codename").Property("name").Value returns the name of the vbComponent, not the tab name of the sheet.
 
Upvote 0
VBComponent("codename").Property("index").Value returns the index of the sheet (i.e. position from the left of the tabs)
VBComponent("codename").Property("name").Value returns the name of the vbComponent, not the tab name of the sheet.

Thanks again.

Doesn't seem to make a difference for my purposes whether I use "Name" or "Index".
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top