CODENAME and selecting sheets through VBA

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
Hello all.

I generally do:
dim ThisBook as workbook
dim ThisSheet as worksheet

set thisbook = activeworkbook
set thissheet =thisbook.worksheets("MudFace") ' Get it?

Anyway, suppose I have a string variable holding the CODENAME of a sheet.

How would I go about selecting the sheet (for processing BTW) in code?
Would like to avoid enumeration.

Thanks!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Unfortunately, I don't know of another way, that isn't a For loop:

Code:
For Each Ws in TheBook.Worksheets
   If Ws.CodeName = "TheCodeName" Then Exit For
Next Ws
 
Upvote 0
Wait, there looks that there is a way (Be careful in XP, because it needs access to the VBProject)

<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> SheetName(Wb <SPAN style="color:#00007F">As</SPAN> Workbook, CodeName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    SheetName = Wb.VBProject.VBComponents(CodeName).Properties("Name").Value
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestIt()
    <SPAN style="color:#00007F">Dim</SPAN> St <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> Wb <SPAN style="color:#00007F">As</SPAN> Workbook
    
    <SPAN style="color:#00007F">Set</SPAN> Wb = ActiveWorkbook
    St = "Hoja1"
    MsgBox SheetName(Wb, St)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

The function SheetName returns the name of the sheet as you see it in Excel.. then, you could use it like

Set Ws = Wb.Sheets(SheetName(Wb, TheCodeName))
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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