LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Excel Worksheet Codenames 2

Thanks for the Help.

After some research I solved the problem as follows:

Sub alpha()
' Declare the variables
Dim VBComp As VBComponent, iExistCntr As Integer
Dim iNewCntr As Integer, sOldCodeName As String
Dim sNewCodeName As String
Dim sht As Worksheet, shtNewSheet As Worksheet
Dim iShtCntr As Integer
' Count the existing sht with name starting "Sheet"
iExistCntr = 0
For Each VBComp In ThisWorkbook.VBProject.VBComponents
If VBComp.Type = vbext_ct_Document Then
If Left(VBComp.Name, 5) = "Sheet" Then
iExistCntr = iExistCntr + 1
End If
End If
Next VBComp
' Add the worksheet
ThisWorkbook.Sheets.Add
' Re-count the existing sht with name starting "Sheet"
iNewCntr = 0
For Each VBComp In ThisWorkbook.VBProject.VBComponents
If VBComp.Type = vbext_ct_Document Then
If Left(VBComp.Name, 5) = "Sheet" Then
iNewCntr = iNewCntr + 1
If iNewCntr = iExistCntr + 1 Then
sOldCodeName = VBComp.Name
End If
End If
End If
Next VBComp
' Count sht that have been renamed
iShtCntr = 0
For Each sht In ThisWorkbook.Sheets
If Left(sht.CodeName, 6) = "MyCdNm" Then
iShtCntr = iShtCntr + 1
End If
Next sht
' Develop the new codename
If iShtCntr = 0 Then
sNewCodeName = "MyCdNm" & "Sheet1"
Else
sNewCodeName = "MyCdNm" & "Sheet" & iShtCntr + 1
End If
' Name the new worksheet
ThisWorkbook.VBProject.VBComponents(sOldCodeName). _
Name = sNewCodeName
End Sub

This code allows me to add new worksheets and control their order via the
codename that I assign.

I hope that it someone might find it usefull.

Regards,

Alasdair Stirling
 
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
Loops with sheet codenames NEMB Excel Discussion (Misc queries) 0 February 13th 11 12:34 AM
Excel Worksheet Codenames Alasdair Stirling Excel Programming 6 November 2nd 04 07:33 AM
Using worksheet codenames dan Excel Programming 0 January 22nd 04 09:46 PM
From my Addin, referring to activeworkbook's sheets by their codenames Dianne Excel Programming 5 August 27th 03 04:36 PM
strange worksheet.codenames in XL97 Arne[_2_] Excel Programming 0 July 28th 03 03:35 PM


All times are GMT +1. The time now is 04:51 AM.

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"