Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default refering to a sheet by codename

I struggled a lot with this issue as the codename returned by VBA wasn't correct! A new sheet wasn't matching its codename in my project, ex.: a sheet named Sheet13 had a codename like Sheet21, and declaring the VBProject didn't make it for me. Worksheets("Sheet13").codename didn't even return "Sheet21", but the codename of another sheet. Quite confusing.

So in the end, I got the code name with this:

dim VBComp As VBIDE.VBComponent
dim SheetName as string, SheetCodeName as string

SheetName = Activesheet.name

For Each VBComp In ThisWorkbook.VBProject.VBComponents
If VBComp.Type = vbext_ct_Document Then
If SheetName = VBComp.Properties.Item(7) Then
SheetCodeName = VBComp.Name
Exit For
End If
End If
Next

Thought I would share, although you may all be retired by now!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default refering to a sheet by codename

When you add sheets is the best time to edit its codename...

With ActiveWorkbook.Sheets.Add
.Name = "SheetName": .CodeName = "SheetCodeName"
End With

...or set an object variable to each sheet and ref by sheetname...

Dim wks As Worksheet
Set wks = Sheets("SheetName")
wks.CodeName = "SheetCodeName"

...if you're looping sheets in a workbook.

If you're assigning codenames based on existing sheetnames then you
might be better to store a set of delimited strings and work through
the set in order. (This requires the strings be sync'd)

Const sNames$ = "Sheet1,Sheet2,Sheet3,Sheet4"
Const sCodeNames$ =
"Sheet1Codename,Sheet2Codename,Sheet3Codename,Shee t4Codename"

Dim i%, vaNames, vaCodeNames

vaNames = Split(sNames, ","): vaCodeNames = Split(sCodeNames, ",")
For i = LBound(vaNames) To UBound(vaNames)
ActiveWorkbook.Sheets(vaNames(i)).Codename = vaCodeNames(i)
Next

Also, you might want to note that you can expect sheetnames to be
changed whereas codenames are likely to persist. In this case you can
use the codename to find a sheetname...

Dim wksSource As Worksheet
Set wksSource = Sheets(Get_SheetTabName(ActiveWorkbook,
"SheetCodeName")

Function Get_SheetTabName(Wkb As Workbook, CodeName As String) As
String
Dim Wks As Worksheet
For Each Wks In Wkb.Worksheets
If Wks.CodeName = CodeName Then _
Get_SheetTabName = Wks.name: Exit Function
Next
End Function

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sheet codename not working Makelei Excel Programming 6 June 1st 10 09:13 PM
Selecting Sheet By Codename Kris_Wright_77 Excel Programming 4 December 16th 09 04:23 PM
Hide sheet via codename [email protected] Excel Programming 3 September 13th 06 08:28 PM
change sheet codename Gary Keramidas Excel Programming 4 March 5th 06 12:54 AM
Using sheet codename problems Dustin Carter Excel Programming 1 February 20th 04 10:26 PM


All times are GMT +1. The time now is 02:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"