Get sheet name from composite code name

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,356
Is there a simple way of retrieving the worksheet name from the code name when the code name = "Sheet" & "1", "2", "3", etc. I can do it by looping through all worksheets, but there must be an easier way.
Thanks in advance for any suggestions.
 
Hi Rick,

Many thanks for the code. NIt doesn't do exactly what I want, but I can easily adapt it to identify code name "Sheet1" and get the sheet name and it is neater than the loop I'm currently using.
Much appreciated.
How about this function then? Just pass it the code name (as a text string) and it will return the sheet name for that code named sheet...
Code:
[table="width: 500"]
[tr]
	[td]Function GetSheetName(CodeName As String) As String
  Dim WS As Worksheet
  For Each WS In Worksheets
    If WS.CodeName = CodeName Then
      GetSheetName = WS.Name
      Exit Function
    End If
  Next
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi MAIT. Thanks for the code. I'm referring to Code Names because I don't know that the Sheet Names in the workbook will always be as expected. I am getting the user to use "Move" to dump the worksheets into my workbook and then I want to work on them one by one. I don't know where the user will dump them and the only way I can identify them is by their new Code Names, Sheet1, etc. All of the sheets in my workbook have had their code names changed to something useful.
 
Upvote 0
I want to run some code on 9 imported worksheets which will automatically receive the code names Sheet1, Sheet2, etc. xCount is a variable which loops through the numbers 1 to 9. At each iteration, I want to know the sheet name of the sheets with code names "Sheet1", "Sheet2", etc.

Just coming back to this comment, and a little curious ...

How are you importing the worksheets, and why can't you determine the worksheet name as each is imported, rather than the roundabout solution of matching worksheet names to the default codenames?
 
Upvote 0
Hi Stephen. I have to rely upon the user to import the sheets and I am asking him to use the Move command to dump them all at once. That will be the easiest for him.
 
Upvote 0
Try this:
It will only show a list of the sheets which have not been given names.
Code:
Sub Sheet_Names()
Dim ans As String
Dim i As Long
    For i = Sheets.Count To 1 Step -1
   If Sheets(i).Name = Sheets(i).CodeName Then ans = Sheets(i).CodeName & vbNewLine & ans
    Next
    MsgBox ThisWorkbook.Name & vbNewLine & "Sheet names are:" & vbNewLine & ans
    
End Sub
 
Last edited:
Upvote 0
Hi Stephen. I have to rely upon the user to import the sheets and I am asking him to use the Move command to dump them all at once. That will be the easiest for him.

So one alternative would be to simply loop once through all worksheets in the workbook, testing if ws.Codename Like "Sheet*".
 
Upvote 0
Hi Stephen. That's more or less what I am doing except that there are nine worksheets being imported. I had hoped to find a simple one line solution, but it does not appear to be possible.
 
Upvote 0
If there are always nine sheets imported and they are always imported in the same order, then the Index property of the sheet can be used. You can probe the Index of the last sheet before the imported 9 and then use Sheets(baseIndex + n) to refer to the nth sheet.
 
Upvote 0
Hi Mike. Nice to hear from you. I had thought of doing that, but it does require the user to put the new worksheets at the end. I have absolutely no confidence that the user will always do that so the Index property will not be reliable. I'm trying to make it foolproof even though I know that the fools will always outwit me in the end.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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