My guess is that the code name is accessible only after the VBA code
has been compiled. Since compilation doesn't occur during the
execution of a procedure, the code name isn't there.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
On Sun, 12 Apr 2009 19:36:02 -0700, AN <(E-Mail Removed)>
wrote:
>Working on a project I found following fact:
>1. CodeName for the added Worksheet is not available/assigned immediately at
>run-time.
>2. Executing the same piece of the code when VBA IDE is open gives different
>result (which is understandable).
>
>Is there any explanation to the finding?
>
>Thanks,
>AN
>
>Code to test:
>
>Sub AddWorksheets()
>Dim WBook As Workbook
>Dim WSheet As Worksheet
>Dim i As Integer
>
> ' Open Log File
> Open ThisWorkbook.Path & "\" & "AddWorksheets_" & Format(Now,
>"yyyy.mm.dd_hh.mm.ss") & ".txt" For Output As #3
> Print #3, "******************************************"
> Print #3, Now
>
> ' create new workbook
> ' and save it and close
> Set WBook = Workbooks.Add
> WBook.SaveAs Filename:=ThisWorkbook.Path & "\External.xlsm",
>FileFormat:=xlOpenXMLWorkbookMacroEnabled, ReadOnlyRecommended:=True
> WBook.Close False
> Set WBook = Nothing
>
> Application.DisplayAlerts = False
> ' open saved
> Set WBook = Workbooks.Open(Filename:=ThisWorkbook.Path & "\External.xlsm")
> Application.DisplayAlerts = True
> ' add to log
> Print #3, WBook.Name & " Add Worksheets..."
> ' add few worksheets to the new workbook
> WBook.Worksheets.Add After:=WBook.Worksheets(Worksheets.Count), Count:=3
> ' add count to log
> Print #3, "WBook.Worksheets.Count: " & WBook.Worksheets.Count
> ' Adding Index, CodeName and Name of each Worksheet to log
> Print #3, "WBook contains:"
> For Each WSheet In WBook.Worksheets
> Print #3, vbTab & "Index: " & WSheet.Index & " WSheet.CodeName:
>" & WSheet.CodeName & " WSheet.Name: " & WSheet.Name
> Next WSheet
> ' close log
> Close #3
>
> WBook.Close False
> Set WBook = Nothing
> Kill ThisWorkbook.Path & "\External.xlsm"
>End Sub
>
>Results:
>1. At run-time:
>******************************************
>12/04/2009 10:07:44 PM
>External.xlsm Add Worksheets...
>WBook.Worksheets.Count: 6
>WBook contains:
> Index: 1 WSheet.CodeName: WSheet.Name: Sheet1
> Index: 2 WSheet.CodeName: WSheet.Name: Sheet2
> Index: 3 WSheet.CodeName: WSheet.Name: Sheet3
> Index: 4 WSheet.CodeName: WSheet.Name: Sheet4
> Index: 5 WSheet.CodeName: WSheet.Name: Sheet5
> Index: 6 WSheet.CodeName: WSheet.Name: Sheet6
>
>The CodeName is absent.
>
>2. With VBA IDE open.
>******************************************
>12/04/2009 10:02:36 PM
>External.xlsm Add Worksheets...
>WBook.Worksheets.Count: 6
>WBook contains:
> Index: 1 WSheet.CodeName: Sheet1 WSheet.Name: Sheet1
> Index: 2 WSheet.CodeName: Sheet2 WSheet.Name: Sheet2
> Index: 3 WSheet.CodeName: Sheet3 WSheet.Name: Sheet3
> Index: 4 WSheet.CodeName: Sheet4 WSheet.Name: Sheet4
> Index: 5 WSheet.CodeName: Sheet5 WSheet.Name: Sheet5
> Index: 6 WSheet.CodeName: Sheet6 WSheet.Name: Sheet6
>
>The CodeName is present.